This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes java awt setLocationRelativeTo with multiple monitors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "java awt setLocationRelativeTo with multiple monitors" Watch "java awt setLocationRelativeTo with multiple monitors" New topic
Author

java awt setLocationRelativeTo with multiple monitors

Venki Umaashankar
Greenhorn

Joined: Apr 26, 2008
Posts: 6
I have Jpanel which is maximized on a button click. I do it by setting the dimensions to the screensize and then calling setLocationRelativeTo(null). This works fine if I have the panel in primary monitor, but when the panel is on other monitor the button click always makes the panel to move to primary monitor. Is there a way to get the currently used monitor of the panel and use the same for new location. Thanks in advance.

I have also posted this question in Stack overflow also.
http://stackoverflow.com/questions/13631335/java-awt-setlocationrelativeto-with-multiple-monitors-p

And I will make sure to edit the question, once I have the answer. Thanks for your time
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Please BeForthrightWhenCrossPostingToOtherSites (link).

P.S. I'm on a roll this week catching crossposters!


The quieter you are, the more you are able to hear.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

There is, but you'd have to use GraphicsEnvironment, GraphicsDevice and GraphicsConfiguration. You'd need to find the GraphicsConfiguration object that best matches the location of the window.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Venki Umaashankar
Greenhorn

Joined: Apr 26, 2008
Posts: 6
Rob Spoor wrote:There is, but you'd have to use GraphicsEnvironment, GraphicsDevice and GraphicsConfiguration. You'd need to find the GraphicsConfiguration object that best matches the location of the window.


Hi,
I have seen these links, but the problem is the window is shifting from one window to other on the button click. There is no issue if the Panel is in primary monitor already, since the button click moves the window to the primary monitor. To be more clear, I want to identity the monitor in which the panel is currently, user could move it between monitors. I want to find where it is currently and set the new location also to the same. Could some one please guide how this could be done using GraphicsConfiguration? I could not figure it out from the Javadocs. Thanks in advance.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

GraphicsConfiguration has method getBounds() which returns the bounds of the matching screen. A window has method getBounds() which returns the bounds of the window. Likewise, you can calculate the bounds of any component by using getBounds() and then changing the top left corner to the result of getLocationOnScreen().

You then need to find which GraphicsConfiguration has a return value of getBounds() that best matches your window's or component's bounds. You should be careful that it's possible for a window or component to be on more than one screen.
Venki Umaashankar
Greenhorn

Joined: Apr 26, 2008
Posts: 6
Rob Spoor wrote:GraphicsConfiguration has method getBounds() which returns the bounds of the matching screen. A window has method getBounds() which returns the bounds of the window. Likewise, you can calculate the bounds of any component by using getBounds() and then changing the top left corner to the result of getLocationOnScreen().

You then need to find which GraphicsConfiguration has a return value of getBounds() that best matches your window's or component's bounds. You should be careful that it's possible for a window or component to be on more than one screen.


Thanks Rob. Your suggestion and following this SO thread helped

http://stackoverflow.com/questions/1248386/how-do-i-determine-which-monitor-a-swing-mouse-event-occurs-in
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java awt setLocationRelativeTo with multiple monitors
 
Similar Threads
question on JPanel
Need some help regarding Hint button in my swt panel
JPanel to JPanel in a JFrame
regarding JPanel
Auto Maximise GUI