• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to limit 1 instance of a Window ?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 :-)
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic