Sciology = Science + Technology

Commonsense in Technology

  • September 2007
    M T W T F S S
     12
    3456789
    10111213141516
    17181920212223
    24252627282930
  • Top Posts

  • Subscribe

  • Follow Me!

  • Blog Stats

    • 275,668 hits
  • Archives

  • Pictures

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

52 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. 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 🙂

  6. […] Positioning Dialogs on Windows […]

  7. 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.

    • kioria said

      Hi, Naquaduh,
      you have an error in your code. Please consider the revised getCenterPoint() method below – without brackets in the second parameter of a Point constructor:
      Shell parentShell = PlatformUI.getWorkbench()
      .getActiveWorkbenchWindow().getShell();
      Rectangle shellBounds = parentShell.getBounds();
      rec.setLocation(shellBounds.x + shellBounds.width / 2,
      shellBounds.y + shellBounds.height / 2);

      With respect, kioria

  8. 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.

  9. 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.

  10. 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.

  11. 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!

  12. 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

  13. 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.

  14. 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!

  15. I am really delighted to read this webpage posts which includes tons of valuable facts, thanks for providing these information.

  16. Arlene said

    There is certainly a great deal to learn about
    this topic. I like all the points you’ve made.

  17. Can I simply say what a comfort to find an individual who really knows what they are discussing on the internet.
    You actually know how to bring a problem to light and make it important.
    More people really need to check this out and understand this side of the story.

    It’s surprising you aren’t more popular since you
    certainly have the gift.

  18. Voyage said

    Bonjour, je suis Danielle
    Je suis une fille de vingt-neuf piges j’assume totalement mon age .
    Je bosse comme chanteuse ! il apparaît que je suis réservée.

  19. website said

    They are also offered in numerous materials
    like cooper and brass and like the other manufacturers, but I do not get in the mainstream
    as well. The term” cheap garage Sliding Patio Doors, commercial entry systems and industrial access. As these Sliding Patio Doors and windows can be more than 50% corrupt before it will open up, but you should limit the mirror to one of the worst rewards. But this fact is true in Shani Shingnapur, a small seat or a wide bench and a wall cabinet for placing the shower essentials.

  20. Piece of writing writing is also a excitement, if you be
    familiar with after that you can write or else it is complicated to write.

  21. quick weight loss said

    Outstanding post but I was wanting to know if you could write a litte more on this topic?
    I’d be very thankful if you could elaborate a little bit further. Appreciate it!

  22. Maxine said

    I am extremely impressed with your writing skills and also with the layout on your weblog.
    Is this a paid theme or did you customize it yourself?
    Anyway keep up the excellent quality writing, it is
    rare to see a nice blog like this one nowadays.

  23. Carrie said

    Hello to all, how is everything, I think every one is getting
    more from this web site, and your views are good in favor of new viewers.

  24. Hi there, I read your blogs regularly. Your writing style is awesome, keep it
    up!

  25. Carri said

    Ιf some one needs expert view regarding blogging afterward i recommeոd him/her to pay
    a visit this blog, Keep up the fastidious work.

  26. download said

    Oh my goodness! Awesome article dude! Many thanks, However I am having
    troubles with your RSS. I don’t understand why I can’t join it.
    Is there anybody else having the same RSS problems? Anybody who knows the answer can you kindly respond?

    Thanks!!

  27. First off I would like to say exxcellent blog! I had a quick question
    which I’d like to ask if you ddo not mind. I was interested tto find out
    how you center yourself annd clear your thoughts
    prior tto writing. I have had difficulty clearing my thoughts in gettung
    my ideas out there. I do take pleasure in writing but it just seems like the first 10 tto
    15 minutes tend too be wasted simply just trying to figure
    out how tto begin. Any suggestions orr hints? Cheers!

  28. Attractive section of content. I just stumbled upon your weblog and in accession capital to
    assert that I acquire actually enjoyed account your blog
    posts. Any way I’ll be subscribing to your feeds and even I achievement you
    access consistently quickly.

  29. We are a group of volunteers and opening a new scheme in our community.
    Your site provided us with valuable info to work on. You’ve performed
    an impressive task and our whole community shall be
    grateful to you.

  30. Your style is really unique compared to other folks I have read
    stuff from. Thanks for posting when you have the opportunity, Guess I’ll just bookmark this page.

  31. A fascinating discussion is worth comment. I do believe
    that you ought to write more about this topic, it may nnot be a taboo matter but
    typically people do not discuss thesxe topics. To the next!

    Cheers!!

  32. That is very fascinating, You’re a very professional blogger.

    I have joined your feed and sit up for in quest of extra of your
    excellent post. Additionally, I’ve shared your web site in my social networks

  33. Hey! I’m at work surfing around your blog from my new apple iphone!
    Just wanted to say I love reading through youjr blog and look forward to alll your
    posts! Carry on thhe excellent work!

  34. Christen said

    Visit to purchase Computer Cuisine Deluxe for $29.
    Never use any shortcut methods while cooking, handling
    and cleaning cookware unless you are hundred percent sure of its success.
    The storage of the pans is far easier when you
    may have this potential at your disposal.

  35. Nice post. I was checking constantly this blog and I am impressed!
    Very helpful info specifically the last section :
    ) I handle such info much. I used to be looking for this
    certain info for a very long time. Thanks and best of
    luck.

  36. Please let me know if you’re looking for a writer for your weblog.
    You have some really great posts and I believe I would
    be a good asset. If you ever want to take some of
    the load off, I’d love to write some articles for your blog in exchange for a
    link back to mine. Please send me an email if interested.
    Cheers!

  37. Terrific work! This is the type of info that are meant to be shared around the internet.
    Disgrace on Google for no longer positioning this publish upper!
    Come on over and consult with my site . Thanks =)

  38. This is a topic that’s near to my heart…
    Many thanks! Exacctly where are your contact details
    though?

  39. Hello, its fastidious article on the topic of meria print,
    we all understand media is a impressive source
    of facts.

  40. Having read this I believed it was rather informative.
    I appreciate you finding the time and energy to
    put this article together. I once again find myself personally spending way
    too much time both reading and commenting. But so what, it was still
    worthwhile!

  41. Netflix, Blockbuster Online, and Intelliflix are just
    a few in the business that can be worth their weight in gold
    in the long run. There are plenty of sites on the Internet that
    offer links to online movies while other sites allow visitors to rent
    them and watch them directly on that site. Of course, you’ve got more options if you’re willing to go a less traditional route
    than popcorn.

  42. The first three months Bollywood suffered a loss of around Rs 200
    crore. The i – Phone prank phenomenon is a strange development in the world of i
    – Phone applications, but those that actually fake phone calls are definitely one of the top
    five unusual uses for the i – Phone. Two issues that were brought up
    were the risks involved in unprotected sex.

  43. We absolutely love your blog and find almost all of your post’s to be just what I’m
    looking for. Does one offer guest writers to write content for you personally?
    I wouldn’t mind creating a post or elaborating on most of the subjects you write regarding here.
    Again, awesome website!

  44. Isabella said

    This is very interesting, You’re a very skilled blogger.
    I have joined your feed and look forward to seeking more of your excellent post.
    Also, I have shared your site in my social networks!

  45. Ebon Talifarro

    How to position Eclipse Dialogs and Messages at ceter of screen. « Sciology = Science + Technology

  46. haushalt said

    It’s actually a cool and useful piece of information. I’m satisfied that you simply shared this useful info with
    us. Please keep us up to date like this. Thanks for sharing.

  47. What blog sites would be good for a small business in floral and gifts?

  48. Sandalay Resort Pattaya Photos

    How to position Eclipse Dialogs and Messages at ceter of screen. « Sciology = Science + Technology

  49. 95zz9108.Com

    How to position Eclipse Dialogs and Messages at ceter of screen. « Sciology = Science + Technology

  50. Thanks for sharing your thoughts about Omvendt mobiltelefonbog.
    Regards

  51. software said

    Wow, that’s what I was exploring for, what a information!
    existing here at this webpage, thanks admin of this
    site.

Leave a reply to Carri Cancel reply