Closing JFrames through button - swing

I have used many methods like hide();, setvisibility and all. But they are not working.
How can I close it dynamically by clicking on another frame button?
I have used all things which are below on button but not working:
rest1.Disp ds = new rest1.Disp();
ds.setVisible(true);
rest.Cashier c = new rest.Cashier();
c.hide();
c.setVisible(false);
c.setDefaultCloseOperation(HIDE_ON_CLOSE);
c.setDefaultCloseOperation(EXIT_ON_CLOSE);
c.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

Step 1.
You should call the setDefaultCloseOperation during initialization of your JFrame. This tells the system how it should respond when the 'X' (close) button of your JFrame is clicked.
It has an integer parameter that can take 4 possible values:
DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do
anything; require the program to handle the operation in the
windowClosing method of a registered WindowListener object.
HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the
frame after invoking any registered WindowListener objects.
DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide and
dispose the frame after invoking any registered WindowListener
objects.
EXIT_ON_CLOSE (defined in JFrame): Exit the application
using the System exit method. Use this only in applications.
It sounds like DISPOSE_ON_CLOSE is what you are looking for - it will hide and dispose the JFrame on which the 'X' button was clicked.
So, in the initialization of your JFrame, call
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Further reading: https://docs.oracle.com/javase/tutorial/uiswing/components/frame.html
Step 2.
When you have your JFrame set up to be disposed on closing, you can also close it programmatically. You can use
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
as explained in this answer.
What happens then is that dispatchEvent will send the WINDOW_CLOSING event to the frame. The system knows it should dispose the frame, because you told it to DISPOSE_ON_CLOSE.
So, you should put that command in the ActionListener for the button that you use to close the frame:
someButton.addActionListener(
new ActionListener( )
{
public void actionPerformed(ActionEvent e)
{
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}
}
);
Together, these two things accomplish that your frame is closed from a button other than the normal 'X' closing button.

Related

How to determine if the right mouse button is down or not in a MOUSE_MOVE event?

For the MOUSE_MOVE event, the documentation says there is a buttonDown property to indicate whether or not the left mouse button is currently down. But how can I determine if the right button is down?
There is not, but you can do this by setting a flag inbetween Right Mouse down and Right mouse up. If you listen on capture with a high priority, it will be available in all other mouse events.
In your document class or main timeline frame 1, add the following code:
var isRightMouseDown:Boolean = false;
stage.addEventListener(MouseEvent.RIGHT_MOUSE_DOWN, globalMouseDown,true,int.MAX_VALUE)
function globalMouseDown(e:MouseEvent):void {
isRightMouseDown = true;
}
stage.addEventListener(MouseEvent.RIGHT_MOUSE_UP,globalMouseUp,true,int.MAX_VALUE)
function globalMouseUp(e:MouseEvent):void {
isRightMouseUp = false;
}
Now you have a var you can access in your mouse move listeners. If using timeline code, access it outside the main timeline by doing MovieClip(root).isRightMouseDown. If using a document class, define it as static public static var isRightMouseDown:Boolean and access it like so from anywhere in your app: MyMainClassName.isRightMouseDown. (replace MyMainClassName with whatever you've called your document class)
When you add the listeners above, putting the third parameter as true and the fourth parameter as int.MAX_VALUE will ensure this listener will be processed before any others listening for the same event in your application.
For more information about how events work and their phases, see this:
http://www.adobe.com/devnet/actionscript/articles/event_handling_as3.html

Applying action to a button in one frame makes it apply to whole movieclip in Flash CS6

I have a movieclip with several layers and frames. Some of those contain buttons. When I click button 1 I want it to go to the next frame. When I click button 2 I want it to go to frame 23. This is the code I'm using for button 1:
this.addEventListener(MouseEvent.CLICK, convnext);
function convnext(evt:MouseEvent):void
{
MovieClip(parent).gesprekkencivcen.nextFrame();
}
and this is the code for button 2:
this.addEventListener(MouseEvent.CLICK, convend1);
function convend1(evt:MouseEvent):void
{
MovieClip(parent).gesprekkencivcen.gotoAndStop(23);
}
What happens now is that when I click either of the buttons, or in fact anywhere inside the movieclip (even layers I haven't applied actions to) it executes both functions at the same time so I end up going to frame 24. Can somebody provide an answer to this problem?
Obviously this in both cases refers to the same object, and not to the buttons in particular. Record the names of those buttons as you've named their instances on the timeline, say button1 and button2 and write the code employing those names.
button1.addEventListener(MouseEvent.CLICK, convnext);
function convnext(evt:MouseEvent):void
{
parent.parent.gesprekkencivcen.nextFrame();
}
button2.addEventListener(MouseEvent.CLICK, convend1);
function convend1(evt:MouseEvent):void
{
parent.parent.gesprekkencivcen.gotoAndStop(23);
}
With this, however, you will need to update the link to gesprekkencivcen in both listeners, as those buttons will have this as parent, and their target apparently is not a child of this. I have tried to plainly set a call to parent.parent.gesprekkencivcen, which might not work.

removing movieclip after executed

okay, i have this code on frame 1
addEventListener(Event.ENTER_FRAME, changeframe);
function changeframe(event:Event):void
{
btsakhir.y -= 6;
if (btsakhir.y <= 56)
{
nextFrame();
}
}
but when i test it whenever it goes to the next frame it show an Cannot access a property or method of a null object reference. but if in the frame 2 i add the instance btsakhir its not error. but all i want in the frame 2 is, there is no btsakhir, can someone give me solution for this so there is no error when going to frame 2 without btsakhir
What is happening is that when flash goes to the next frame, the Event.ENTER_FRAME event handler is still running. And it expects that there is an object called "btsakhir" with a y property. You can test this by adding a trace in your changeFrame() method and you should see it still tracing, even if it is on the next frame.
Two things you could do:
1.) change your event handler (your changeframe() method) to check and account for when "btsakhir" no longer exists on the next frame. This may get a little messy and not recommended depending on how you choose to do it.
Or
2.) remove the event listener so that it isn't running at all when you go to the next frame. If all the changeframe() method does is check "btsakhir"'s y property, this is would probably a better way to go. To remove the event listener you can add:
this.removeEventListener(Event.ENTER_FRAME, changeframe);
You would add it just before you call nextFrame();
Of course if your object "btsakhir" is supposed to exist on the next frame, (and is an object on the timeline/stage), just add frames to it.

Procedure to use the result from a textbox inside a popup - ActionScript

I am relatively new to ActionScript (have started with it 2 months ago), and have a little doubt of 'procedure' or 'technique' related to passing information between objects.
I have made a class that Pops-up a window that contains a panel with a textbox and two buttons, one for accepting, other for cancelling. It should work as a prompt in which you enter some text, and then if you like the changes, you accept, else, you cancel and the text you entered is discarded.
The thing I'm not sure how to handle is how to receive the text, once the user presses 'Accept', from the class I want to receive it from.
So, the approach I took is a bit cumbersome: firstly, when launching the popup, I associate with it a function (called onResult() in the code) from the 'class that launches', which will be called after the user presses the 'Accept' or 'Cancel' buttons; secondly, to get the text that the user inserted in the box, I keep a reference to it public from my class.
Please have a look at the code here:
http://pastebin.com/Kmud8rBe
I've also programmed in Android before, and the approach there would be much cleanier, just putting the text result from the popup inside a bundle inside an intent, and receiving it from the launched class. Here, I have to pass functions and such, which I don't like at all (although it works!).
So, my question is, for you ActionScript gurus out there, how would you approach this?
Thanks and regards!
pepillo
good that you created a class for your popup-functionality but why did you make all functions static? normal class with normal methods would be better ... and let the class extend from Sprite so that you can add the instance right to the stage.
package
{
import flash.display.Sprite;
public class Popup extends Sprite
{
public function Popup (label:String)
{
// add text and buttons ...
}
}
}
then you can just say:
var pop:Popup = new Popup("message");
addChild(pop);
and to get the data back after the popup is closed you would do sth like this:
private function onPressedAccept(event:MouseEvent):void
{
var text:String = _label.text;
// dispatch a custom event which saves the text as its data
dispatchEvent(new MyEvent(MyEvent.ACCEPT, text));
// close popup ...
parent.removeChild(this);
// or you would remove the popup in the ACCEPT eventlistener ...
}
listening for accept/cancel:
var pop:Popup = new Popup("message");
addChild(pop);
// add eventlistener
popup.addEventListener(MyEvent.ACCEPT, onAccept);
popup.addEventListener(MyEvent.CANCEL, onCancel);
private function onAccept(event:MyEvent):void
{
trace(event.data);
}
link about creating custom events:
http://www.8bitrocket.com/2007/7/31/Creating-Custom-Events-In-Flash-AS3-ActionScript-3/

Action Script 3 On Click

Before i start i want to let you know today is my first day with AS3.
I wanted to know how to do an onclick function in AS3.
For example i have button 1 ( as Instance name)
and when clicked i want it to hide and show another box. this is what i found online but how can i make it on click.
this.button1.alpha = 100;
Thanks so much.
You want
button1.addEventListener(EventType, callback);
You replace EventType with a mouse event (such as MouseEvent.MOUSE_DOWN) and callback is a function that you define, which is called whenever the event occurs.
See the following example, taken from this page on the FlashEnabled Blog:
// attach the event listener to this object, if you want a global event outside
// the current class attach to stage.addEventListener([event],[callback])
this.addEventListener(MouseEvent.CLICK, onMouseClickEvent);
// then make the callback
public function onMouseClickEvent(event:Event) {
trace(event);
if(event.buttonDown)
// if primary button down, left mouse button
trace(”left button was down”);
else
trace(”left button was not down”);
}
}
The above code sample attaches a click event handler to this (whatever context this code is executed in - it could be global, or inside a class). Inside your event handler, you'd want to use the Tween class (as explained on Kirupa.com) to animate the box out and the other box in.
Since you mentioned that it's your first day, note that trace() writes to the console.