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?

Recommended Answers

All 9 Replies

without seeing any code? doubt it.

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??"

you are not looking over the whole application. you are talking about two applications running simultaneously. that's a bit a different matter.

Are you asking about executing the program in more than one JVM? On the same PC?

does seem that way to me.

You could use a socket to detect if another version of a program is running on a PC.

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;
    private MainFrame()
        superDao = SuperDAO.getInstance();

        tabs = new JTabbedPane();
        //populate tabs
        //setSize(500, 500);

    public static MainFrame getInstance()
        if(theInstance == null)
            theInstance = new MainFrame();
        return theInstance;
//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
            //another MainFrame

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.