Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #5097

Re: How to select the monitor a shell is put on (SWT)

From Cecil Westerhof <Cecil@decebal.nl>
Newsgroups comp.lang.java.gui
Subject Re: How to select the monitor a shell is put on (SWT)
Date 2012-03-18 11:36 +0100
Organization Decebal Computing
Message-ID <87ehsqxkju.fsf@Compaq.site> (permalink)
References <87mx7fxtll.fsf@Compaq.site> <jk2tmh$i06$1@dont-email.me>

Show all headers | View raw


Op zaterdag 17 mrt 2012 21:56 CET schreef Jeff Higgins:

> On 03/17/2012 09:08 AM, Cecil Westerhof wrote:
>> I am learning to work with SWT. I have two monitors. How can I select
>> the monitor where a Shell is instantiated?
>>
> I don't know about that. But you can locate a shell using
> Display.getMonitors(), and then Monitor.getClientArea(),and
> Shell.setLocation().

I first used:
	Rectangle bds = shell.getMonitor().getBounds();
	Point     p   = shell.getSize();

	int nLeft = (bds.width - p.x)  / 2;
	int nTop  = (bds.height - p.y) / 2;

	shell.setBounds(nLeft, nTop, p.x, p.y);

On your tip I used:
	Monitor primary = display.getPrimaryMonitor ();
	Rectangle bounds = primary.getBounds ();
	Rectangle rect = shell.getBounds ();

	int x = bounds.x + (bounds.width - rect.width) / 2;
	int y = bounds.y + (bounds.height - rect.height) / 2;
	shell.setLocation (x, y);

But that had the same effect.

In debug mode I found that an object of type Monitor has the following
fields:
    clientHeight
    clientWidth
    clientX
    clientY
    handle
    height
    width
    x
    y

Sadly no identifying information.

With display.getMonitors() I found out that the first monitor
contains:
    1680
    2650
    0
    0
    0
    1680
    1050
    0
    0

And the second:
    900
    1600
    1050
    0
    1
    900
    1600
    1050
    0

So the first has in client Height/Width the combined values and the
second only its own. I want my shell on the second one, so I need to
use:
	Monitor[] monitors = display.getMonitors();
	Monitor monitorToUse = monitors[1];
	Rectangle monitorRect = monitorToUse.getBounds();
	Rectangle shellRect = shell.getBounds();
	int x = monitorRect.x + (monitorRect.width - shellRect.width) / 2;
	int y = monitorRect.y + (monitorRect.height - shellRect.height) / 2;
	shell.setLocation (x, y);

Now I have to build something that can save the preferred monitor.
(Including checks if it refers to a still existing one.)

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

How to select the monitor a shell is put on (SWT) Cecil Westerhof <Cecil@decebal.nl> - 2012-03-17 14:08 +0100
  Re: How to select the monitor a shell is put on (SWT) Jeff Higgins <jeff@invalid.invalid> - 2012-03-17 16:56 -0400
    Re: How to select the monitor a shell is put on (SWT) Cecil Westerhof <Cecil@decebal.nl> - 2012-03-18 11:36 +0100
      Re: How to select the monitor a shell is put on (SWT) "John B. Matthews" <nospam@nospam.invalid> - 2012-03-18 17:24 -0400

csiph-web