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

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;
}

This entry was posted in eclipse, java, plugins and tagged , . Bookmark the permalink.

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

  1. Hi,

    It’s true that SWT opens windows almost everywhere 🙂

    But your code can be disturbing with dual monitor setups, because it always open new windows on the first monitor, even if the application is used on the secondary one.

    Because of this, I usually use the bounds of the parent window to compute the coordinates of the new window, and fall back to the bounds of the monitor if there is no parent shell.

    I believe most applications behave this way.

    Nicolas

  2. Alex Blewitt says:

    Yeah, this is majorly annoying when I have an Eclipse window on my second screen and it shows up a dialog on the first screen for no good reason.

    It will also suck when you’re giving a presentation on how to use Eclipse with an OHP and it suddenly freezes because there’s a modal dialog waiting on your laptop screen that you might not even be able to see from where you’re standing…

    Plus, screens aren’t necessarily the same size. Centering it on the active window is a much better idea; and will give centred on the screen when the window is maximised anyway.

  3. Hi Nicolas,
    Yes i got the point… i mean if we know the parent shell which is invoking the child, then we are good. Some how i get into some instances where there is no parent shell (true for most of message dialogs).
    OK…now i am thinking…do you know how do we get the monitor in which eclipse application is running ? (in a dual monitor system)
    ~Krishna

  4. Why not doing simply display.getBounds() ?

    It works for me.

  5. Hey, nice tips. I’ll buy a bottle of beer to that person from that chat who told me to go to your site 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *