I want to programmatically simulate pressing the X (close) icon on the
right-top corner of a JFrame, so that this will cause a "WindowClosing"
event the same way it happens when I actually click on the icon. It seems
JFrame.setVisible(0) doesn't do what I want.
Is there a way to do this?
Thanks
Bora
ak - 08 Jan 2004 22:41 GMT
> I want to programmatically simulate pressing the X (close) icon on the
> right-top corner of a JFrame, so that this will cause a "WindowClosing"
> event the same way it happens when I actually click on the icon.
you have to write your own class extending JFrame:
public class MyFrame extends JFrame {
public void doClose() {
WindowEvent evt = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
processWindowEvent(evt);
}
}
> It seems
> JFrame.setVisible(0) doesn't do what I want.
JFrame.setVisible(0) doesn't exist - JFrame.setVisible(false);
--
____________
http://reader.imagero.com the best java image reader.
kodonne2 - 08 Jan 2004 23:47 GMT
try this.dispose(); - i think it does the same thing.
ak - 09 Jan 2004 02:05 GMT
> try this.dispose(); - i think it does the same thing.
I am not sure, because if you set
JFrame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE) then you can't close
window by pressing close, but you can close or dispose it programmatically.
____________
http://reader.imagero.com the best java image reader.
Adam - 09 Jan 2004 07:15 GMT
> I want to programmatically simulate pressing the X (close) icon on the
> right-top corner of a JFrame, so that this will cause a "WindowClosing"
> event the same way it happens when I actually click on the icon. It seems
> JFrame.setVisible(0) doesn't do what I want.
You can also try with Robot class, to simulate actual mouseclick.
Adam
Thomas Kellerer - 09 Jan 2004 07:30 GMT
Bora schrieb:
> I want to programmatically simulate pressing the X (close) icon on the
> right-top corner of a JFrame, so that this will cause a "WindowClosing"
> event the same way it happens when I actually click on the icon. It seems
> JFrame.setVisible(0) doesn't do what I want.
Write a doClose() method which does what you want (e.g. setVisible(false)).
Use this method to close your window programatically. In the windowClosing()
event, simply call your doClose() method.
Much cleaner then trying to invoke system events.
Thomas