Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JFrame

 
Raja Kannappan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good news. It appears that
singletonFrame.setState( Frame.NORMAL );
works as well and is pre-1.4.
Good Luck.
 
Raja Kannappan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. That method exists in JDK1.3.
Thanks Dirk!
- Raja.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic