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

Nicolas Richeton said
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
Alex Blewitt said
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.
sureshkrishna said
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
Antoine Toulme said
Why not doing simply display.getBounds() ?
It works for me.
How to Get Six Pack Fast said
Hey, nice tips. I’ll buy a bottle of beer to that person from that chat who told me to go to your site
JFace Dialogs : which one is right for you ? » Sciology said
[...] Positioning Dialogs on Windows [...]
Naquaduh said
This will get the center Point for the active shell;
public Point getCenterPoint() { Shell parentShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); Rectangle shellBounds = parentShell.getBounds(); return new Point(shellBounds.x + shellBounds.width / 2, (shellBounds.y + shellBounds.height) / 2); }Then simply override the getInitialLocation method of Dialog;
@Override protected Point getInitialLocation(Point initialSize) { Point shellCenter = getCenterPoint(); return new Point(shellCenter.x - initialSize.x / 2, shellCenter.y - initialSize.y / 2); }Done.
site said
Wanted to drop a comment and let you know your Feed is not functioning today. I tried adding it to my Yahoo reader account and got nothing.
how to make your own greenhouse fast said
If some one needs to be updated with most recent technologies
after that he must be go to see this web site and be up to date all the time.
original URL said
What’s up to every body, it’s my first go to see of this blog;
this webpage contains remarkable and genuinely fine stuff in favor of visitors.
conair pet clippers said
magnificent put up, very informative. I’m wondering why the opposite specialists of this sector do not notice this. You should proceed your writing. I’m confident, you have a great readers’ base already!
Immigration Advicers in Westminister said
Hi there! Would you mind if I share your blog with my myspace group?
There’s a lot of people that I think would really enjoy your content. Please let me know. Thank you
seo tips said
Does your blog have a contact page? I’m having trouble locating it but, I’d like to shoot
you an e-mail. I’ve got some recommendations for your blog you might be interested in hearing. Either way, great site and I look forward to seeing it expand over time.
car-covers.com said
Wow, marvelous blog format! How lengthy have you been blogging for?
you make blogging glance easy. The full look of
your website is wonderful, let alone the content!
Buy Anti Aging Cream said
I am really delighted to read this webpage posts which includes tons of valuable facts, thanks for providing these information.