Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JFrame one instance

 
Andraz Poje
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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);
 
Paul Clapham
Sheriff
Pie
Posts: 20958
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
Christiaan Lombard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

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

 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the UI forum.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic