Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Centering a Dialog on Screen
Centering a Dialog on Screen [message #465049] Sun, 04 December 2005 22:52 Go to next message
Jeff Yuan is currently offline Jeff YuanFriend
Messages: 34
Registered: July 2009
Member
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] Mon, 05 December 2005 00:15 Go to previous messageGo to next message
Daniel Spiewak is currently offline Daniel SpiewakFriend
Messages: 263
Registered: July 2009
Senior Member
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] Mon, 05 December 2005 01:00 Go to previous messageGo to next message
Jeff Yuan is currently offline Jeff YuanFriend
Messages: 34
Registered: July 2009
Member
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] Mon, 05 December 2005 04:03 Go to previous messageGo to next message
Daniel Spiewak is currently offline Daniel SpiewakFriend
Messages: 263
Registered: July 2009
Senior Member
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 14: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: Mon Dec 16 13:50:08 GMT 2019

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

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

Back to the top