Java SWT application - Bring To Front
Asked Answered
U

4

9

I am currently developing an SWT java application on Windows 7. Usually the application will be minimized, and when there is an event on the serial port the application should maximizes itself. The following code does the maximization part.

private void bringToFront(final Shell shell) {
    shell.getDisplay().asyncExec(new Runnable() {
        public void run() {
            if(!shell.getMaximized()){
                shell.setMaximized(true);
            }
            shell.forceActive();
        }
    });
}

But sometimes the SWT application is maximized behind another application. For example, if I have a powerpoint running in Fullscreen mode maximized application is behind powerpoint presentation. I would like to get it maximized and bring in front of all other applications.

Can anyone help me?

Unscrew answered 15/3, 2012 at 15:29 Comment(1)
Nope. The forceActive method moves the shell to the top of the Z order. This will bring the shell to the front if and only if no other window is also moving itself to the top of the Z order.Corneliacornelian
I
9

There is a another "hackey" way to do this than what you found, which does not require you to minimize everything else. You actually need to call shell.setMinimized(false) and after that shell.setActive() to restore the previous state of the shell. However, that only works if the shell was truely in the minimized state. So here is my final solution, which artificially minimizes the shell if it was not minimized already. The cost is a quick animation if the minimization has to be done.

shell.getDisplay().syncExec(new Runnable() {

    @Override
    public void run() {
        if (!shell.getMinimized())
        {
            shell.setMinimized(true);
        }
        shell.setMinimized(false);
        shell.setActive();
    }
});
Irtysh answered 25/8, 2015 at 10:39 Comment(0)
D
5

You need to set the style bit SWT.ON_TOP on your Shell instance. Unfortunately setting style bits is only possible in the constructor.

But if I understand your use case setting that bit might be viable for you, since you only seem to toggle between minimized and maximized state.

If that's not possible, simply dispose and re-create your shell and its contents, when you want to toggle between states.

Dedicate answered 15/3, 2012 at 21:32 Comment(0)
U
4

I found some workaround for the problem, might not be the best solution but works for me. If someone have better solution keep posted. Thanks

Using the method showDesktop() first simulate windows key + D event to show the desktop

     private void showDesktop()  {  
       try{  
          Robot robot = new Robot();  
          robot.keyPress(KeyEvent.VK_WINDOWS);  
          robot.keyPress(KeyEvent.VK_D);  
          robot.keyRelease(KeyEvent.VK_D);  
          robot.keyRelease(KeyEvent.VK_WINDOWS);  
          }  
        catch(Exception e){e.printStackTrace();}  
     }

Then maximize the shell application

    private void bringToFront(final Shell shell) {

         showDesktop(); //minimize all the application

         Thread.sleep(5000); // here have to wait for some time, I am not sure why

         shell.getDisplay().asyncExec(new Runnable() {
         public void run() {
             if(!shell.getMaximized()){
                shell.setMaximized(true);
             }
             shell.forceActive();
         }
    });
  }
Unscrew answered 20/3, 2012 at 8:30 Comment(0)
P
-2

You need your application to work in full screen mode.

look at this link How can I create a Java Swing app that covers the Windows Title bar?

Par answered 15/3, 2012 at 15:34 Comment(1)
ok, I am not using SWING library but SWT. I tried with similar option on SWT but it does not meet what I expect. The java application when maximized is still behind the power point application.Unscrew

© 2022 - 2024 — McMap. All rights reserved.