Path: csiph.com!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: Cecil Westerhof Newsgroups: comp.lang.java.gui Subject: Re: How to select the monitor a shell is put on (SWT) Date: Sun, 18 Mar 2012 11:36:37 +0100 Organization: Decebal Computing Lines: 85 Message-ID: <87ehsqxkju.fsf@Compaq.site> References: <87mx7fxtll.fsf@Compaq.site> NNTP-Posting-Host: lYLrXPFpjyZ8237ODaoN+w.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: abuse@aioe.org User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux) X-Notice: Filtered by postfilter v. 0.8.2 Cancel-Lock: sha1:iX8pqF0DVkKF07eSje/o4lEwJLc= X-Homepage: http://www.decebal.nl/ Xref: csiph.com comp.lang.java.gui:5097 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