• Post Reply Bookmark Topic Watch Topic
  • New Topic

Screen Position  RSS feed

 
Dilip kumar
Ranch Hand
Posts: 360
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm writing a java application using Swing components. By default the window appears at the top-left corner of the monitor. Is there any way/function through which we can display the screen at the center ?
Thanks
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Window inherits the method setLocation from Component, so any Component that you happen to be using can use this:
w.setLocation(50, 60);
For multi-screen situations you need something more like:
Window w = new Window(Window owner, GraphicsConfiguration gc);
Rectangle bounds = gc.getBounds();
w.setLocation(10 + bounds.x, 10 + bounds.y);
See the API:Window for all the gory details.
 
Noah Carroll
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way to do it is by calling the method that will return the screen size with current monitor settings. The method is in the window class so any frames or windows can use it. You can access from your main frame by
Dimension wndSize = mainFrame.getToolkit().getScreenSize();
The method getToolkit() returns a Toolkit object that has many useful methods in it, getScreenSize() being one of them. getScreenSize returns a Dimension object, so to center a 400 X 400 frame do this
mainFrame.setBounds(wndSize.width/2-200, wndSize.height/2-200, 400, 400);
------------------
I hope its helps, feel free to email me noahcarroll@juno.com
[This message has been edited by Noah Carroll (edited January 24, 2001).]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!