Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generating swt shell at the center of its parent shell

Tags:

java

swt

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?

like image 541
mayur_mitkari Avatar asked Feb 20 '13 12:02

mayur_mitkari


2 Answers

Rectangle screenSize = display.getPrimaryMonitor().getBounds();
shell.setLocation((screenSize.width - shell.getBounds().width) / 2, (screenSize.height - shell.getBounds().height) / 2);
like image 182
john01dav Avatar answered Sep 20 '22 22:09

john01dav


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));
like image 26
Ben Holland Avatar answered Sep 18 '22 22:09

Ben Holland