Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java SWT application - Bring To Front

Tags:

java

swt

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?

like image 858
Dinesh Avatar asked Mar 15 '12 15:03

Dinesh


2 Answers

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();
    }
});
like image 181
milez Avatar answered Sep 30 '22 04:09

milez


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.

like image 28
the.duckman Avatar answered Sep 30 '22 06:09

the.duckman