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();