开发者

only one swing frame window opened at time

开发者 https://www.devze.com 2022-12-24 16:09 出处:网络
I developed one swing application but each time you run application new window is opened. I wa开发者_Python百科nt that if one window is already opened other not allow to open. Here is an example of a

I developed one swing application but each time you run application new window is opened. I wa开发者_Python百科nt that if one window is already opened other not allow to open.


Here is an example of a Java Single Application Instance:

A single instance application is one that only allows for 1 of the application to run no matter how many times the user tries to launch.

See also: A shorter example that does not notify the running instance.

The application tries to open a Socket on a specific port. In case another instance of your application is already running, opening the Socket fails.

This should already be sufficient for you, so you would not have to use the part of the code used to register new applications to the first one started.

Using a Socket has one great advantage compared to writing some sort of flag to the filesystem/registry/whatever:
It is removed even if your application crashes.


It actually sounds like you only want one application open at a time. In which case why not take out a file lock or similar when the application runs, and check that on start up. The headache (of course) is clearing up that lock in the event that your program doesn't exit cleanly.


My preferred solution is, as Peter Lang linked to, to use Sockets. When your app starts you can start a server socket listening for incoming connections on localhost (plus port of your choice). Before this happens in your code though you can try and make a connection to the server socket and if it is successful you know there is another instance already open, so you can quit the current instance with an appropriate message.

In your server socket implementation you can also add functionality that on receiving an incoming connection you actually force the current instance of the app to the foreground.


Do you mean run the GUI like a Singleton?, I have done this in the past by making a Static private "view manager" such that it is null and not created or visible until the first time the gui is created, after that just as with a classic singleton, the GUI is set to visible when the app is run again... I have a couple of Frameworks that follow this design--In these frameworks the GUI is not "primary" there are also command line and the like interfaces so the GUI is summoned via the command line...


public class Samp {

    JFrame f=new JFrame();
    File ff=new File("D:\\a.txt");
    FileWriter fw;

    public Samp() {
        f.setBounds(0, 0, 200, 200);       
        try {
            Scanner sc=new Scanner(ff);
            if(!sc.hasNext()) {         
                fw=new FileWriter(ff);
                fw.write("Running");
                fw.close();
            } else {
                System.exit(0);
            }
        } catch(Exception e) {
            System.out.println(e.getMessage());
        }

        WindowListener wndCloser = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                try {
                    Scanner sc = new Scanner(ff);
                    if(sc.hasNext()) {
                        fw=new FileWriter(ff);
                        fw.write("");
                        fw.close();
                    }
                } catch (Exception ex) { }
            }
        };

        f.setVisible(true);
        f.addWindowListener(wndCloser);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String args[]) {
        new Samp();
    }
}


Use Singletone Pattern as shown in the example!

0

精彩评论

暂无评论...
验证码 换一张
取 消