«« Next » « Previous
«« Next » « Previous

Link Details

Link 5313 thumbnail
User 200547 avatar

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.
  • 7
  • 0
  • 1891
  • 275

Comments

Add your comment
User 111696 avatar

bloid complained ago:

2 votes Vote down Vote up Reply

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...

User 107114 avatar

daniel replied ago:

0 votes Vote down Vote up Reply

I actually didn't know about setLocationRelativeTo(null) Nice tip!

User 200547 avatar

codebeach replied ago:

0 votes Vote down Vote up Reply

Thanks for the tip bloid! The tutorial has been updated to show this for Java 1.4 or newer.

User 32201 avatar

rguy replied ago:

0 votes Vote down Vote up Reply

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.

User 107114 avatar

daniel replied ago:

0 votes Vote down Vote up Reply

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.

User 111696 avatar

bloid replied ago:

0 votes Vote down Vote up Reply

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...

User 185591 avatar

seanf replied ago:

2 votes Vote down Vote up Reply

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());
}

User 111696 avatar

bloid replied ago:

0 votes Vote down Vote up Reply

Great Tip seanf!! :-D Thanks for that!

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.

Voters For This Link (7)



Voters Against This Link (0)