Java SWT application - Bring To Front

前端 未结 4 1218
深忆病人
深忆病人 2021-02-15 03:23

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 m

4条回答
  •  深忆病人
    2021-02-15 03:41

    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();
             }
        });
      }
    

提交回复
热议问题