1

I am trying to center a dialog in my Eclipse RCP application, and I tried different methods to center the dialog.

Is there an easy/accepted way to center the dialog respecting the current location of the primary shell (the workbench app If I am not mistaken)? I can center the dialog on the primary monitor, but I am unable to center the dialog respecting the main app window.

This is a problem on multi-monitor setups. I start the app and it is displayed on the primary monitor. If I move the app to the second screen, I am failing to center the dialog not on the primary monitor but on the second screen where the app is.

---- Update 1 ----

At the moment every dialog is created with a new Shell:

Shell shell = new Shell(Display.getCurrent(), SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
        PortOverviewDialog portOverviewDialog = new PortOverviewDialog(shell);
        portOverviewDialog.show();

I think this is the main problem. How can I avoid this by getting the primary Shell?

---- Update 2 ----

For me the code Shell shell = HandlerUtil.getActiveShell(event); gets the parent Shell. And I am setting the Dialog to "parent center" with the help of the following code:

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));
Fabian Deitelhoff
  • 712
  • 1
  • 7
  • 23
  • 1
    As long as you use the main Shell as the parent for the dialog this should be automatic. – greg-449 Mar 24 '22 at 11:50
  • That's a nice hint. I don't think that is the case. The previous developers created a new shell for every new dialog as I can see in the code (I updated my question). Which is the best way to get the main Shell so I can set it as the parent? – Fabian Deitelhoff Mar 24 '22 at 11:59
  • 1
    One way is `Display.getActiveShell`. In an old style command handler you can use `HanderUtil.getActiveShell`. In an e4 handler or other injected class you can inject `IServiceConstants.ACTIVE_SHELL` – greg-449 Mar 24 '22 at 12:43
  • Thanks! In my case it is ```Shell shell = HandlerUtil.getActiveShell(event);``` which does the trick. So it is necessary to set the parent correct and create a new Shell for the Dialog itself? The current implementation creates a new Shell and sets it as a parent and as the current shell for the Dialog. What I learned in the last hour from your comments is, that this is completely wrong, isn't it? – Fabian Deitelhoff Mar 24 '22 at 13:19
  • And I updated my question with the code I am using to center the Dialog as "parent center". This is not done automatically. Is there another way, which avoids the custom code? – Fabian Deitelhoff Mar 24 '22 at 13:21
  • 1
    You do not need to create any Shells. The dialog creates its own shell and centers that on its parent shell. Just pass the active shell to the constructor of your dialog. – greg-449 Mar 24 '22 at 14:14
  • Oh, thanks! Sounds logical. – Fabian Deitelhoff Mar 24 '22 at 14:28

0 Answers0