I am using to different classes: one holding a main JFrame with a button, and one holding a new JFrame that is called upon at a button press.
if( event.getSource() == noteBtn ) { MiniPad.pad(); return;}
(MiniPad.pad() references the class and pad() method on the new JFrame)
When I removeAll() on the JPanel that hosts the button, and then revalidate() and repaint(), the button opens the JFrame multiple times, which isn’t what I want it to do at all.
Is there a way to tell the MiniPad class that you can’t have more than one copy of the JFrame open at any one time? I extend the JFrame by the way, in case that’s any help.
Edit: Everything below is valid programming knowledge, but you might also want to consider having
MiniPadextend theJDialogclass instead. I haven’t used it before, but its implementation looks a lot likeJFrame. You might not actually have to change much in yourMiniPadclass. The documentation is here: http://docs.oracle.com/javase/7/docs/api/javax/swing/JDialog.htmlIf you’re wondering why, check out Andrew Thompson’s post here.
—
From what I understood of your question,
MiniPadextendsJFrame, and thepad()method creates a new instance of theMiniPadclass. The simplest solution would be to turn theMiniPadclass (at least through thepad()method) into a singleton. A singleton is a type of class where only one instance (or object) can exist at any given time. By calling a static method (in this casepad()) you check to see if an instance of the object already exists; if it does, simply use that existing object:This should do what you want. By calling the
pad()method, only oneMiniPadwill ever show up.However, if I read your question wrong, let me know and I will revise my answer.
Info on singletons:
http://en.wikipedia.org/wiki/Singleton_pattern