generating swt shell at the center of its parent shell
Asked Answered
F

5

10

I have SWT wizard page as my parent shell , for creating another shell on click of button i am writing 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 as the child shell means this shell is not placed at the center of SWT wizard that is parent shell why?

Fishwife answered 20/2, 2013 at 12:3 Comment(3)
Did you check if bounds and rect really contain the correct information?Heteroousian
Baz , yes it contains informationFishwife
But is the information correct?Heteroousian
C
8
Rectangle screenSize = display.getPrimaryMonitor().getBounds();
shell.setLocation((screenSize.width - shell.getBounds().width) / 2, (screenSize.height - shell.getBounds().height) / 2);
Candor answered 18/10, 2013 at 5:59 Comment(1)
This displays the dialog centred on the screen, not over the parent as requested in the question.Chine
K
4

If you are writing a dialog or a child component you may want to use getParent instead of asking the display for the primary monitor so that the window is centered on the current screen for multiple monitor setups.

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));
Kranz answered 3/12, 2016 at 21:14 Comment(1)
My problem exactly, this should be the accepted answer because nowadays multiple monitor setups are not rare, and the "+parentSize.x;" is crucial and it's missing in the other answer.Bentlee
G
0

I think the best would be to use style SWT.SHEET for such dialog.

Galloping answered 16/10, 2017 at 8:48 Comment(0)
S
0

I just ran into this same problem. The solution I got is a little different. This might help others with the same issue. The context is a shell (hoverShell) that hovers over the parent (shell) for a couple of seconds displaying a message.

    private void displayMessage(Shell shell, String message) {
      Shell hoverShell = new Shell(shell, SWT.ON_TOP);
      hoverShell.setLayout(new FillLayout());
      Label messageLabel = new Label(hoverShell, SWT.NONE);
      messageLabel.setText(message);
      Point shellLocation = shell.getLocation();
      hoverShell.pack();
      hoverShell.setLocation(shellLocation.x + (shell.getSize().x / 2) - (hoverShell.getSize().x / 2), shellLocation.y + 40);
      hoverShell.open();
      Display.getDefault().timerExec(2000, new Runnable() {

        @Override
        public void run() {
            hoverShell.dispose();
        }
    });
}

In a nutshell this is the following formula:

child_location.x = parent_location.x + .5*(parent_width.x) - .5*(child_width.x)

If you want y then it's the exact same I believe. May depend if the top border of the window is calculated.

Seminarian answered 21/11, 2019 at 10:12 Comment(0)
R
0

tried, tested and working code:

int width = display.getClientArea().width;
int height = display.getClientArea().height;
shell.setLocation(((width - shell.getSize().x) / 2) + display.getClientArea().x, ((height - shell.getSize().y) / 2) + display.getClientArea().y);
Rasure answered 9/9, 2022 at 8:11 Comment(1)
need to add code blocksRemonstrate

© 2022 - 2024 — McMap. All rights reserved.