Question

I'm working on a Swing UI in which I want to center multiple components (JDialogs and JFrames). I know that the following code will calculate the user's screen size, and from there, I can easily center a component:

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

For efficiency's sake, I should only calculate this once and store it in some kind of constant so it can be reused in any part of the project. What is the best practice for storing this for later reuse so it is accessible across multiple classes?

(Furthermore, if there is a better way of calculating screen size for centering, I'd be open to hearing that as well)

Was it helpful?

Solution

java.awt.Window.setLocationRelativeTo(null) will center it on the screen whilesetLocationRelativeTo(someComponent) will center it relative to the java.awt.Component, someComponent.

One thing to consider with the alternate of storing the center, is that if a user adjusts their resolution while the program is running than the stored constants will no longer be valid. Is recalling the getScreenSize function actually expensive? (I do not know whether or not it is)

OTHER TIPS

This puts the upper left corner of the component on center, but not the whole component

This means the size of the dialog/frame is (0, 0), Your basic code should be:

frame.add( .... );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );

For centering the object you should try:

The frame position in X = (half frame width) - (half screen size width) Almost the same for Y = (half frame height) - (half screen size height)

You can easily stores the values in the main class with public access, so you don't need to read them several times

Also, if you do it yourself, you need to factor in the screen's insets using Toolkit.getScreenInsets to account for things like the task bar, which might be on any screen edge and be of any size.

Before I could target Java 1.4, I used:

static public void centerWindow(Window wnd, Component relcom) {
    Rectangle                           scrbnd=getScreenBounds(wnd);
    Dimension                           wndsiz=wnd.getSize();
    Container                           root=null;
    int                                 px,py;

    if(relcom!=null) {
        if(relcom instanceof Window || relcom instanceof java.applet.Applet) {
            root=(Container)relcom;
            }
        else {
            Container parent;
            for(parent=relcom.getParent(); parent!=null; parent=parent.getParent()) {
                if(parent instanceof Window || parent instanceof java.applet.Applet) {
                    root=parent;
                    break;
                    }
                }
            }
        }

    if(relcom==null || !relcom.isShowing() || root==null || !root.isShowing()) {
        px=(scrbnd.x+((scrbnd.width -wndsiz.width )/2));
        py=(scrbnd.y+((scrbnd.height-wndsiz.height)/2));
        }
    else {
        Point       relloc=relcom.getLocationOnScreen();
        Dimension   relsiz=relcom.getSize();

        px=(relloc.x+((relsiz.width -wndsiz.width )/2));
        py=(relloc.y+((relsiz.height-wndsiz.height)/2));
        }

    if((px+wndsiz.width )>(scrbnd.x+scrbnd.width )) { px=((scrbnd.x+scrbnd.width )-wndsiz.width ); }
    if((py+wndsiz.height)>(scrbnd.y+scrbnd.height)) { py=((scrbnd.y+scrbnd.height)-wndsiz.height); }
    if(px<scrbnd.x) { px=scrbnd.x; }
    if(py<scrbnd.y) { py=scrbnd.y; }
    wnd.setLocation(px,py);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top