Java SWT Application - Bring to the Front

I am currently developing a SWT Java application on Windows 7. Typically, the application will be minimized, and when there is an event on the serial port, the application should maximize 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 after another application. For example, if I have a powerpoint running in full screen mode, the maximum application is behind a PowerPoint presentation. I would like to get it as much as possible and execute in front of all other applications.

Can anybody help me?

+6
source share
4 answers

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

But if I understand that your use case, this bit may be useful to you, because you only switch between the minimized and maximized states.

If this is not possible, simply delete and recreate your shell and its contents when you want to switch between states.

+5
source

There is another way of โ€œhackingโ€ to do this than what you find that does not require you to minimize everything else. You really need to call shell.setMinimized(false) and after that shell.setActive() restore the previous state of the shell . However, this only works if the shell was truthfully in a minimized state. So, here is my final solution, which artificially minimizes the shell , if it has not been minimized already. Cost is a quick animation if minimization is to be done.

 shell.getDisplay().syncExec(new Runnable() { @Override public void run() { if (!shell.getMinimized()) { shell.setMinimized(true); } shell.setMinimized(false); shell.setActive(); } }); 
+5
source

I found some solution to the problem, maybe not the best solution, but works for me. If anyone has a better solution, post it. Thanks

Using the showDesktop () method, first simulate a windows + D event window 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 increase 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(); } }); } 
+4
source

You need your application to work in full screen mode.

look at this link How to create a Java Swing application that spans the Windows title bar?

-2
source

Source: https://habr.com/ru/post/910810/


All Articles