Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java awt setLocationRelativeTo with multiple monitors

 
Venki Umaashankar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please BeForthrightWhenCrossPostingToOtherSites (link).

P.S. I'm on a roll this week catching crossposters!
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Venki Umaashankar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic