Generating a swt shell in the center of its parent shell

I have a SWT wizard page as my parent shell, to create another shell when I click a button, I write the following code

Shell permissionSetShell = new Shell(Display.getCurrent().getActiveShell(), SWT.CENTER|SWT.DIALOG_TRIM|SWT.APPLICATION_MODAL); permissionSetShell.setText(PropertyClass.getPropertyLabel(QTLConstants.PERMISSION_SET_COLUMN_LABEL)); // Add shell to the center of parent wizard permissionSetShell.setLayout(componentsRenderer.createGridLayout(1, false, 0, 5, 0, 0)); Monitor primary = Display.getCurrent().getPrimaryMonitor (); Rectangle bounds = primary.getBounds (); Rectangle rect = Display.getCurrent().getActiveShell().getBounds (); int x = bounds.x + (bounds.width - rect.width) / 2; int y = bounds.y + (bounds.height - rect.height)/2; permissionSetShell.setLocation (x, y); 

but since the child shell means that this shell is not placed in the center of the SWT wizard, which is the parent shell, why?

+6
source share
3 answers
 Rectangle screenSize = display.getPrimaryMonitor().getBounds(); shell.setLocation((screenSize.width - shell.getBounds().width) / 2, (screenSize.height - shell.getBounds().height) / 2); 
+7
source

If you are writing a dialog or a child component, you can use getParent instead of asking for a display for the main monitor so that the window is focused on the current screen for several monitor settings.

 Rectangle parentSize = getParent().getBounds(); Rectangle shellSize = shell.getBounds(); int locationX = (parentSize.width - shellSize.width)/2+parentSize.x; int locationY = (parentSize.height - shellSize.height)/2+parentSize.y; shell.setLocation(new Point(locationX, locationY)); 
+1
source

I think it's best to use the SWT.SHEET style for such a dialogue.

0
source

All Articles