How to set focus the already running application?

前端 未结 2 552
时光说笑
时光说笑 2021-01-15 19:35

I am using a ServerSocket port to run one instance only of my Java Swing application, so if a user tries to open another instance of the program, i show him a warning that \

相关标签:
2条回答
  • 2021-01-15 19:56

    I don't know if this is absolutely right, but here's the final code i've used and it works fine for me :

    public class Loader {
    private static final int PORT = 9999;
    private static ServerSocket serverSocket = null;  // Server
    private static Socket socket = null;  // CLient
    private static final String focusProgram = "FOCUS";
    
    public static void main(String[] args) {
        if( !isProgramRunning() ) {
            Main main = new Main();
            main.setVisible( true );
        }
        else {
            System.exit( 2 );
        }
    }
    
    private static boolean isProgramRunning() {
        try {
            serverSocket = new ServerSocket(PORT,0,InetAddress.getByAddress(new byte[] {127,0,0,1}));  // Bind to localhost adapter with a zero connection queue. 
            SwingWorker<String, Void> anotherThread = new SwingWorker<String, Void>() {  // Do some code in another normal thread.
                @Override
                public String doInBackground() {  // This method is to execute a long code in the other thread in background.
                    serverSocketListener();
                    return "";
                }
            };
            anotherThread.execute();  // Execute the other tread.
        }
        catch (BindException e) {
            System.err.println("Already running.");
            clientSocketListener();
    
            return true;
        }
        catch (IOException e) {
            System.err.println("Unexpected error.");
            e.printStackTrace();
    
            return true;
        }
    
        return false;
    }
    
    public static void serverSocketListener() {  // Server socket
        try {
            System.out.println( "Listener socket opened to prevent any other program instance." );
            socket = serverSocket.accept();
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    
            if( in.readLine().equals( focusProgram ) ) {  // Restore the opened instance however you want.
                Global.getCurrentFrame().setState(Frame.NORMAL);
                Global.getCurrentFrame().toFront();
            }       
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }
    
    public static void clientSocketListener() {  // Client socket
        try{
            socket = new Socket(InetAddress.getByAddress( new byte[] {127,0,0,1}), PORT );
            PrintWriter out = new PrintWriter( socket.getOutputStream(), true );
            out.println( focusProgram );
        } catch  (IOException e) {
            System.out.println("No I/O");
            System.exit(1);
        }
    }
    

    }

    0 讨论(0)
  • 2021-01-15 20:07

    Since you use a server socket I assume that you use the java.net.BindException to detect that you application is already running. If you start a second instance you could send a control message which instructs you first app to normalize (if minimized) before exiting.

    if (msg == BRING_TO_FRONT ) {
       frame.setState(Frame.NORMAL);
       frame.toFront();
    }
    
    0 讨论(0)
提交回复
热议问题