tags:

views:

2793

answers:

6

What's the correct way to get a JFrame to close, the same as if the user had hit the [x] button, or pressed Alt+F4 (on windows)?

I have my default close operation set the way I want, via

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

and it does exactly what I want, with the aforementioned controls. This question isn't about that.

What I really want to do is cause the gui to behave in the same way as a press of [x] would cause it to behave.

I.e., supposing I were to extend WindowAdaptor, and then add an instance of my adaptor as a listener via addWindowListener(); I would like to see the same sequence of calls through windowDeactivated() windowClosing() windowClosed() as would occur with the [x]. Not so much tearing up the window as telling it to tear itself up, so to speak.

EDIT

Wanted to share the results, mainly derived from following camickr's link. Basically I need to throw a WindowEvent.WINDOW_CLOSING at the application's event queue. Here's a synopsis of what the solution looks like

// closing down the window makes sense as a method, so here are
// the salient parts of what happens with the JFrame extending class ..

    public class FooWindow extends JFrame {
        public FooWindow() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(5, 5, 400, 300);  // yeah yeah, this is an example ;P
            setVisible(true);
        }
        public void pullThePlug() {
                WindowEvent wev = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
                Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
        }
    }

// Here's how that would be employed from elsewhere -

    // someplace the window gets created ..
    FooWindow fooey = new FooWindow();
    ...
    // and someplace else, you can close it thusly
    fooey.pullThePlug();
A: 

*setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);*

Not only closes the JFrame but the shutdowns the entire application, hence "EXIT ON CLOSE"

To achieve the same result you have to effectively exit the application, for that simply call

 System.exit(0);

The effect is exactly the same.

OscarRyz
What's with the down vote? : (
OscarRyz
A: 
 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Itay
+3  A: 

If by Alt-F4 or X you mean "Exit the Application Immediately Without Regard for What Other Windows or Threads are Running", then "System.Exit(...)" will do exactly what you want in a very abrupt, brute-force, and possibly problematic fashion.

If by Alt-F4 or X you mean hide the window, then frame.SetVisible(false) is how you "close" the window. The window will continue to consume resources/memory but can be made visible again very quickly.

If by Alt-F4 or X you mean hide the window and dispose of any resources it is consuming, then frame.dispose() is how you "close" the window. If the frame was the last visible window and there are no other non-daemon threads running, the program will exit. If you show the window again, it will have to reinitialize all of the native resources again (graphics buffer, window handles, etc).

Dispose is might as close to the behavior that you really want. If your app has multiple windows open, do you want Alt-F4 or X to quit the app or just close the active window?

The Java Swing Tutorial on Window Listeners may help clarify things for you.

James Schek
+3  A: 
setVisible(false); //you can't see me!
dispose(); //Destroy the JFrame object

Not too tricky.

windfinder
But that doesn’t call event listeners.
Bombe
+5  A: 

If you want the GUI to behave as if you clicked the "X" then you need to dispatch a windowClosing Event to the Window. The "ExitAction" from Closing An Application allows you to add this functionality to a menu item or any component that uses Actions easily.

camickr
Thanks! This was what I was looking for, wouldn't have guessed it on my own.
JustJeff
A: 

Oscar, reason there's a downvote - I'm guessing, it wasn't me :-) is that you need agnostic code that closes the window whatever its default close action is, rather than shutting down the program whenever a window is closed