• 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

JFrame one instance

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I get just one instance of JFrame when I click on JButton?
Every time I click on the button I got a new JFrame over the old one.
I want to use one instance of the frame(and should be the first instance I created it when I click on the button) every time I click on the button
please help me.
 
Sheriff
Posts: 22787
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the singleton design pattern. You can use lazy initialization since you should only create frames from the Event Dispatcher Thread.
 
Andraz Poje
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:Check out the singleton design pattern. You can use lazy initialization since you should only create frames from the Event Dispatcher Thread.


I checked it out, too complicated code examples for newbie like me... Can anyone provide simpler code?

something like that

if (someFrame isvisible==true) put it on top
else someframe.setVisible(true);
 
Marshal
Posts: 28262
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, just like that. Only simpler.

First, create the JFrame when you create all the other components of your GUI. Don't make it visible just yet.

Then, when the controller button is clicked, just make it visible. You don't need to test whether it is visible; if it were visible you wouldn't be able to see the button and click it.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Can't really help you if I can't see your code, but maybe this will help:

 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to the UI forum.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oyu already have a JButton showing, so you must already have a JFrame (JWindow etc) showing,
and when you click the button you only want a single 'other' JFrame to appear.

if this is correct, change the 2nd JFrame to a modal JDialog, which will prevent the JButton from being clicked
whilst the JDialog is showing
reply
    Bookmark Topic Watch Topic
  • New Topic