This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

To have only one JFrame at a time

 
John Meurig
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or a simple boolean switch to do the same.
 
John Meurig
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic