• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

To have only one JFrame at a time

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic