Rawb
Rawb

Reputation: 347

SWT disable window causes lose focus

I'm making custom dialogs that I want to pop up and disable the main shell behind it so that it cannot be clicked while the dialog is active.

My initial plan was something like as follows:

shell.setEnabled(false);
doDialogStuff();
shell.setEnabled(true);

this worked but as I close the dialog, it loses focus of the shell that was open before the dialog. I managed to sort of fix it by adding

shell.setFocus();

after the last line but this is messy and causes the screen to flicker as the window loses and then gains focus in a split second, also, it sometimes doesn't regain focus and I can't understand why :/

Is there a better way to disable the background window without it losing focus.

Thanks in advance SO peeps

Upvotes: 1

Views: 1647

Answers (2)

Baz
Baz

Reputation: 36894

You should create a custom dialog based on this tutorial.

This way you just have to set the modality of the dialog to whatever you need exactly and the dialog will take care of the rest for you.

This should be helpful as well (Javadoc of Shell):

The modality of an instance may be specified using style bits. The modality style bits are used to determine whether input is blocked for other shells on the display. The PRIMARY_MODAL style allows an instance to block input to its parent. The APPLICATION_MODAL style allows an instance to block input to every other shell in the display. The SYSTEM_MODAL style allows an instance to block input to all shells, including shells belonging to different applications.

Upvotes: 2

Geoff Reedy
Geoff Reedy

Reputation: 36011

The proper thing to do is create the dialog as a modal window. When you create the dialog's shell you should do something like

dialogShell = new Shell(mainShell, PRIMARY_MODAL | DIALOG_TRIM);

Upvotes: 1

Related Questions