Tvd
Tvd

Reputation: 4601

Bring a running application to front when other instance is opened

I have made my java application as Single instance application. I have implemented File Lock system for the same.

If the application is already running, I want to show the running application to front. How do I achieve that? How can I acess the running process of that application and show it?

Upvotes: 2

Views: 1732

Answers (2)

Andrew Thompson
Andrew Thompson

Reputation: 168845

JWS not only provides an x-plat SingleInstanceService, but it also (from memory) pops the application toFront() on newActivation(String[]). Of course if it is not automatic, you can call it explicitly.

Here is a demo. of the SIS.

Upvotes: 1

GETah
GETah

Reputation: 21459

What you are asking for is OS dependent but you can always have your own implementation to do this. You application can listen on a certain port for a bring-to-front command that you can send from the second instance of your application.

void main(String[] args){
   if(applicationAlreadyRunning){
     // Send bring-to-front message to running instance on a known port
     // and exit.
   }
}

When a bring-to-front message is received you can do:

public void BringToFrontCommandReceived(){
    java.awt.EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    myMainFrame.toFront();
                    myMainFrame.repaint();
                }
            });

}

Upvotes: 1

Related Questions