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

后端 未结 6 421
逝去的感伤
逝去的感伤 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:51

    private static void onTop(Shell shell) {
            int s = -1;
            Shell[] shells = display.getShells();
            for (int i = 0; i < shells.length; ++i) {
                if (!shells[i].equals(shell)) {
                    shells[i].setEnabled(false);
                    shells[i].update();
                } else {
                    s = i;
                }
            }
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch())
                    display.sleep();
            }
            for (int i = 0; i < shells.length; ++i) {
                if (i != s) {
                    shells[i].setEnabled(true);
                    shells[i].update();
                }
            }
        }
    

提交回复
热议问题