By codebeach
via codebeach.com
Published: Oct 23 2006 / 14:46
Are you tired of the random location of the windows in your application? It is time to take control of your window's position. This tutorial looks at how to center a dialog box, frame, or window in Java.
Comments
bloid complained ago:
bloid reported this link as lame on 10/22/2006 @ 06:15:17
frame.setLocationRelativeTo( null ) ;
does exactly the same thing...in much less code...
daniel replied ago:
I actually didn't know about setLocationRelativeTo(null) Nice tip!
codebeach replied ago:
Thanks for the tip bloid! The tutorial has been updated to show this for Java 1.4 or newer.
rguy replied ago:
setLocationRelativeTo(null) always centers on the main screen though. In a dual screen environement it might be annoying it the users launches the application on the second screen: the frame will show up on the first screen.
daniel replied ago:
Well, like it or not, that's how most applications behave. If you're on a duel-head machine, you can expect certain weirdness like that.
bloid replied ago:
And don't forget that;
frame.setLocationRelativeTo( parentFrame ) ;
works as well to keep your child forms nicely centered around their parents :-)
And yeah, my dual screen environment puts windows spanning the screen whether I do the sLRT() call or the window dimension stuff...
seanf replied ago:
My dual screen gives the same problem. The only working method I've found is to use the Java 1.3 method (from the article), but instead of Toolkit.getScreenSize() I use this implementation of getScreenSize() (requires 1.4):
public static Dimension getScreenSize()
{
GraphicsEnvironment graphicsEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice screenDev = graphicsEnv.getDefaultScreenDevice();
DisplayMode mode = screenDev.getDisplayMode();
return new Dimension(mode.getWidth(), mode.getHeight());
}
bloid replied ago:
Great Tip seanf!! :-D Thanks for that!
Voters For This Link (7)
Voters Against This Link (0)