Only one window swinging frame, open in time

I developed one swing app, but every time you run the app, a new window opens. I want that if one window is already open, the other is not allowed to open.

+5
source share
6 answers

Here is an example Java Single Application Instance :

The only application-application is an application that allows only one of the applications to run, regardless of how many times the user tries to run.

The application is trying to open Socket on a specific port. If another instance of your application is already running, opening Socket fails.

This is already enough for you, so you will not need to use the part of the code used to register new applications before the first launch.

Using Socket has one big advantage compared to writing a flag in the file system / registry / independently:
It is deleted even if the application crashes.

+17
source

Actually, it sounds like you want one application to open at a time. In this case, why not pull out a file lock or the like when starting the application and check that at startup. A headache (of course) clears this lock in case your program crashes.

+1
source

My preferred solution that Peter Lang is associated with is to use Sockets. When the application starts, you can start the server socket, which will listen for incoming connections on localhost (plus the port of your choice). Before this happens in your code, although you can try and establish a connection to the server socket, and if it succeeds, you know that another instance is already open, so you can exit the current instance with the corresponding message.

In the implementation of your server socket, you can also add functionality that, when receiving an incoming connection, will actually make the current application instance go to the forefront.

+1
source
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(); } } 
0
source

You mean running the GUI like Singleton ?, I did this in the past by creating a private Static view manager so that it is empty and not created or visible until the first gui is created after, just like in the case with classic singleton, the graphical user interface becomes visible when the application starts again ... I have a couple of frameworks that follow this design. In this framework, the graphical interface is not the "main", there is also a command line and similar interfaces, so the GUI is called through the command line ...

0
source

Use the Singletone template as shown in the example !

0
source

All Articles