Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2004

Tip: Looking for answers? Try searching our database.

How to programmatically simulate pressing the X (close) icon on the right-top corner of a JFrame

Thread view: 
Bora - 08 Jan 2004 21:04 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.

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.