How do you force a java swt program to “move itself to the foreground”?

后端 未结 6 412
逝去的感伤
逝去的感伤 2020-12-15 22:20

Currently with swt, I sometimes want a program to arbitrarily come to the foreground (like an alarm clock might).

Typically the following works (jruby):



        
6条回答
  •  北海茫月
    2020-12-15 22:30

    There is a way to make it work with what you initially tried. 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();
        }
    });
    

提交回复
热议问题