aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JFrame one instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JFrame one instance" Watch "JFrame one instance" New topic
Author

JFrame one instance

Andraz Poje
Ranch Hand

Joined: Sep 25, 2008
Posts: 32
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

Joined: Oct 27, 2005
Posts: 19781
    
  20

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Andraz Poje
Ranch Hand

Joined: Sep 25, 2008
Posts: 32
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
Bartender

Joined: Oct 14, 2005
Posts: 18985
    
    8

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

Joined: Nov 22, 2009
Posts: 26
Hi

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



SCJP 6
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10532
    
    9

Moving to the UI forum.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JFrame one instance