Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to limit 1 instance of a Window ?

 
Peter Nicholson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no, the windowAdapter is for the gamma window.

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

 
Peter Nicholson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic