I have a class which extends JFrame and this class is made singleton (private constructor, public static method to create instance if null). But still I get one more frame when already one is open. Can someone help?
Sorry ppl...i'm re-framing my question as "When i launch the app, a JFrame loads (created by main thread). When i again launch, a separate main thread creates another JFrame. Is it possible to make the JFrame singleton across the whole application so that at any one point, only one JFrame can be open for the application??"
What do you want to happen in the second launch if it discovers that there is already an open window? Just exit? Request focus for the existing window and exit? Continue executing without a window and communicate somehow with the existing window? Something else (if so what)?
I'm having this same issue. I do not want to highjack the thread, but neither am I sure I should start a new thread for the same problem. If a moderator thinks it is better, then I guess they can remove my reply or something.
I have this (a comment where the issue emerges is included):
public class MainFrame extends JFrame
private static MainFrame theInstance = null;
private static SuperDAO superDao = null;
private JTabbedPane tabs;
superDao = SuperDAO.getInstance();
tabs = new JTabbedPane();
public static MainFrame getInstance()
if(theInstance == null)
theInstance = new MainFrame();
//and then I have this other class, that includes this:
public void actionPerformed(ActionEvent e)
MainFrame.getInstance();//THIS IS WHERE THINGS GO WRONG
//the first time a press the "refresh button", I get another
//MainFrame, and then the button works fine, that is without producing
Alright, I just solved my problem and this might solve the OP's problem as well.
I had a main method inside my MainFrame class. Instead of instantiating a MainFrame object using the getInstance method, I had new MainFrame inside the main method. Seems like the main method has access to the private constructor that is in the same class, and that is why I was able to start my MainFrame while its instance field was null. Anyways, I hope this is of any help to the OP as well.
For Each ctrl As Control In Me.Controls("pnlMainPanel").Controls
If ctrl.GetType Is GetType(System.Windows.Forms.Panel) Then
For Each subCtrl As Control In ctrl.Controls
If subCtrl.GetType Is GetType(System.Windows.Forms.TextBox) Then
If subCtrl.GetType Is ...