GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes To have only one JFrame at a time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "To have only one JFrame at a time" Watch "To have only one JFrame at a time" New topic
Author

To have only one JFrame at a time

John Meurig
Greenhorn

Joined: Apr 28, 2004
Posts: 5
I've got a JButton in JFrame A, which when clicked generates JFrame B. If I click the JButton again, I get two copies of JFrame B.

Is there a way to check for the existing JFrame, and to bring it into focus?
Timo von Oertzen
Greenhorn

Joined: May 14, 2004
Posts: 6
Hi John,

I would do the following: Add a field "JFrame actualB" in the class of JFrame A, and initialize it to null. If the Button is pressed, check whether actualB is null; if so, create B and store it in actualB. If not, call "actualB.toFront()". Code looks somehow like:

If (actualB==null) actualB = new JFrameB();
else actualB.toFront();


Hope it helps,

Timo
Gary Down
Ranch Hand

Joined: Apr 09, 2004
Posts: 46
or a simple boolean switch to do the same.
John Meurig
Greenhorn

Joined: Apr 28, 2004
Posts: 5
Thanks Timo and Gary, it works fine right now
Had to add window listeners to reset the JFrame actualB to null when
it was closed, otherwise it wouldn't work afterward.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: To have only one JFrame at a time