Sciology = Science + Technology

Commonsense in Technology

  • Archives

  • Pictures

Archive for September 5th, 2007

How to position Eclipse Dialogs and Messages at ceter of screen.

Posted by sureshkrishna on September 5, 2007

I want to share a common issues that most of the UI programmers come across. When we program with lots of dialogs and messages in any kind of applications, for the usability and predictability we would want most of them to be centered to the screen. In many cases when we use constructors for Dialogs, MessageDialogs, etc… very often we pass newly constructed Shell.

e.g. MessageDialog.openInfo(new Shell(), “Hi This is a test Message Dialog.”);

In the above case remember that we have not specified any of the co-ordinates and often we see that it takes different coordinates depending on the number of times that we invoke it. And each time you invoke, you wonder where the dialog will appear. I have been using a simple solution that solves this issue… Hope some of you will be able to use this code right away. I hope the following code is self explanatory 🙂

I have tested this with all screen resolutions and on Windows XP and OSX.

/**
* All dialogs and messages will be passed with the centered shell.
* @return Shell
*/
public static Shell getScreenCentredShell() {
Display display = PlatformUI.getWorkbench().getDisplay();
Shell centreShell = new Shell(display);
Point size = centreShell.computeSize(-1, -1);
Rectangle screen = display.getMonitors()[0].getBounds();
centreShell.setBounds((screen.width-size.x)/2, (screen.height-size.y)/2, size.x, size.y);
return centreShell;
}

Posted in Eclipse, Java, Plug-ins, Plugin, RCP, SWT | 52 Comments »