Skip to main content



      Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Centering a Dialog on Screen
Centering a Dialog on Screen [message #465049] Sun, 04 December 2005 17:52 Go to next message
Eclipse UserFriend
Hi,
How can I center a dialog or shell on screen? For example, I have a fixed
size custom Dialog, how can I make it appear in the center of the screen?

Thanks.
Re: Centering a Dialog on Screen [message #465051 is a reply to message #465049] Sun, 04 December 2005 19:15 Go to previous messageGo to next message
Eclipse UserFriend
Child Shell(s) automatically position themselves exactly centered in the parent Shell (which is usually the desired behavior). To manually center any Shell, use the following snippet:

int width = display.getClientArea().width;
int height = display.getClientArea().height;
shell.setLocation(((width - shell.getSize().x) / 2) + display.getClientArea().x, ((height - shell.getSize().y) / 2) + display.getClientArea().y);


Assuming 'shell' is the local field representing your Shell and 'display' is the local field representing shell.getDisplay(), shell will be perfectly centered on the screen agnostic of platform and screen trim (e.g. Linux panels or PocketPC menubars).
Re: Centering a Dialog on Screen [message #465053 is a reply to message #465051] Sun, 04 December 2005 20:00 Go to previous messageGo to next message
Eclipse UserFriend
I tried this code, but it still does not seem centered to me (off to the
top left by quite a bit). I'm using it to try to center a banner that I
load in my program as it loads. So I do have display, and shell is just
the banner that I load.

Do you have any ideas about why it doesn't center?

Thanks.
Re: Centering a Dialog on Screen [message #465055 is a reply to message #465053] Sun, 04 December 2005 23:03 Go to previous messageGo to next message
Eclipse UserFriend
Try this instead:

int width = display.getClientArea().width;
int height = display.getClientArea().height;
shell.setLocation((width - shell.getSize().x) / 2, (height - shell.getSize().y) / 2);


I've removed the compensation for platform specific left/top insets. Not sure why it didn't work properly for you. The above code should though.
Re: Centering a Dialog on Screen [message #465073 is a reply to message #465049] Mon, 05 December 2005 09:21 Go to previous message
Eclipse UserFriend
Originally posted by: kwpeck.mersoft.com

zinc wrote:
> Hi,
> How can I center a dialog or shell on screen? For example, I have a
> fixed size custom Dialog, how can I make it appear in the center of the
> screen?
>
> Thanks.
>

If you are doing this at startup and you don't have a "good" shell /
display (which I did not with my login dialog) then the following code
will center on the 1st monitor in a multiple monitor setup or only
monitor if you only have a single.

Monitor [] ma = display.getMonitors();
if (ma != null)
m_shell.setLocation((ma[0].getClientArea().width - pixel.width) /
2, (ma[0].getClientArea().height - pixel.height) / 2);

In this case I have the height / width of the dialog stored in the
"pixel" variable.
Previous Topic:How to achieve HTTP Post using Ole Automation in SWT
Next Topic:Problem with TableTreeViewer with SWT.CHECK on FEDORA CORE 3 -- HELP NEEDED!
Goto Forum:
  


Current Time: Sat Apr 26 15:03:14 EDT 2025

Powered by FUDForum. Page generated in 0.03470 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top