File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JFrame" Watch "JFrame" New topic
Author

JFrame

Raja Kannappan
Ranch Hand

Joined: May 08, 2002
Posts: 83
In my program, there are three ways by which i can start a JFrame. Assume that they are like three menu items. When i click on them, i start a new JFrame.
What i need is, if that window (JFrame) already exists then i should not create another window. But what is happening now is whenever i click on the menuitem, it starts a new window even though that window already exists.
Is there a way to solve this ?
Please help me regarding this.
Thanks,
- Raja.


SCJP
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Raja,
Welcome to JavaRanch!
You must be very intelligent as your display name complies with The JavaRanch Naming Policy
Thanks Pardner!
------------------------------
Take a look at this thread for an example of using the Singleton Pattern to solve this problem.
Good Luck.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
You need to use the singleton pattern. You will need a get method. That method will check to see if your frame exists if so return, if not create and return. You would then call the get and just show it.
Raja Kannappan
Ranch Hand

Joined: May 08, 2002
Posts: 83
Many thanks for replying me! That was very helpful. I saw a sample code in the thread you indicated. I'm really surprised seeing the forum is so active.
In the singleton pattern program, if the second window is minimized, it is not coming back again when button is clicked. Is there a way to bring the second window back ?
Once again thanks,
- Raja.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I took a quick peek at the the API documentation for Frame and after a bit of fiddling around discovered Frame::setExtendedState( int ).
Here's a modified version the main class in the singleton frame example (referenced above) that sets the extended state of the singleton frame to Frame.NORMAL (and restores a minimized frame):
Raja Kannappan
Ranch Hand

Joined: May 08, 2002
Posts: 83
Dirk,
Thanks for your quick response.
setExtendedState(Frame.NORMAL) methods appears only in JDK1.4. But I'm in a situation that i can use only JDK1.3. So, i couldn't use that method.
I found an alternate method so that i can restore my minimized window. I changed getInstance() method like this:
public static SingletonFrame getInstance()
{
if (frame != null)
{
frame.setVisible(false);
frame = null;
}
frame = new SingletonFrame();
return frame;
}
What i'm doing here is, even if the frame already exists i set it to null and bring a new frame. This way i always get my new window in focus.
Please reply me whether i can do so.
Thanks,
- Raja.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
The obvious effect of doing this would be to destroy the previously created instance (and any state that it had - data settings such as text in text areas or check field settings, etc.). If this works for you, then it works - but I can't help but think that there is a better way.
Let's move this conversation to the Swing / JFC / AWT forum and try to grab the attention of someone with a bit more GUI experience...
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Good news. It appears that
singletonFrame.setState( Frame.NORMAL );
works as well and is pre-1.4.
Good Luck.
Raja Kannappan
Ranch Hand

Joined: May 08, 2002
Posts: 83
OK. That method exists in JDK1.3.
Thanks Dirk!
- Raja.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JFrame