*
The moose likes Swing / AWT / SWT and the fly likes How to limit 1 instance of a Window ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to limit 1 instance of a Window ?" Watch "How to limit 1 instance of a Window ?" New topic
Author

How to limit 1 instance of a Window ?

Peter Nicholson
Greenhorn

Joined: Feb 11, 2012
Posts: 6
Hi,

this is my first post so please be gentle.

I have 3 classes:

* Alpha (main class)

* Beta - a class that displays a window

* Gamma - a class that displays a window

Alpha is running a MouseListener that monitors behaviour in Beta. If the mouse is right-clicked in the Window in Beta, a new Window Gamma appears.

I currently have the following method to ensure that the Window Gamma can only be created once :

public void mouseClicked(MouseEvent e)
{
if(e.getButton() == e.BUTTON3)
{
if(gammaActive != true)
{
Gamma = new Gamma;
gammaActive = true;
}
}
}

So basically if an instance of Gamma exists, do nothing, otherwise create the object and subsequent window.

This is working fine so far, however the problem is, how do I set the boolean gammaActive back to false when the Gamma object is closed/destroyed ?

I'm not sure if I should be trying to determine if the Gamma window has been closed using a WindowListener, or somehow determine if the Gamma object is still on the Heap ?

I may be completely on the wrong path here so I'm open to any suggestions. Please let me know if I have not posted correctly.

Thanks in advance :-)
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I'm not sure if I should be trying to determine if the Gamma window has been closed using a WindowListener, or somehow determine if the Gamma object is still on the Heap ?

resetting it via WndowAdapter's windowClosing() would seem to be the simplest
Peter Nicholson
Greenhorn

Joined: Feb 11, 2012
Posts: 6
Hi Michael,

so do you suggest I setup a Window Adapter in the main class and listen for the window closing in the Gamma class ?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
no, the windowAdapter is for the gamma window.

something like this (this is only the beta/gamma bit)

Peter Nicholson
Greenhorn

Joined: Feb 11, 2012
Posts: 6
Thank you Michael that has done the trick. I'd actually come up with something very similar (which is encouraging as I'm new to Java) , however my code required a double-click rather than a single click - didn't quite have the boolean setting properly.

I've cleaned up my code to reflect your example and it's working a treat.

Thanks again. I can see why this site is so popular.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to limit 1 instance of a Window ?
 
Similar Threads
What does this assignment mean?
Protected in subclasses
Try this Qs !!!
Which code compiles, but throws an exception...confused about the answer