Center Swing Windows
Asked Answered
J

5

16

I'm developing a Java Swing application. How can I make it so that when the program itself and any other windows open they come up in the center of the screen?

Jornada answered 21/6, 2010 at 3:12 Comment(0)
A
46
frame.setLocationRelativeTo( null );
Aesop answered 21/6, 2010 at 3:18 Comment(5)
Does that work also on older versions of Java ? I'm surprised because a while ago I figured out the only way to do that was "by hand", as explained here: java2s.com/Code/Java/Swing-JFC/Howtocenteraframeordialog.htmAllonym
That is valid as of 1.4, but you have to make sure that you pack or otherwise size the screen correctly first.Semiotic
Not really obvious when you don't know it, but the behavior of setLocationRelativeTo(), with a null, is indeed to center the Window, as described in the javadoc.Isadora
It doesn't centre the window - it centres on the top left of the window.Vite
@DanMatthews-Grout, this is because you need to invoke frame.pack() or frame.setSize() BEFORE you invoke frame.setLocationRelativeTo(...). Otherwise the size of the frame is (0, 0) so location is based on that size.Aesop
B
4

frame.setLocationRelativeTo(null); make the frame open in cetner

Regards, Rehan Farooq

Brit answered 4/3, 2013 at 12:14 Comment(0)
E
3

It is easy if you are using Netbeans. In property window of JFrame go to "Code" tab.There is an option as "Generate center".Check that option. JFrame will display in center.

Edge answered 24/7, 2014 at 7:15 Comment(0)
F
1

You'll have to do it by hand, using setLocation(x,y).

Something like:

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((dim.width-frameWidth)/2, (dim.height-frameHeight)/2);

should do it (not tested).

Franchot answered 21/6, 2010 at 3:44 Comment(0)
G
1

Doing it by hand in multi-screen environment gives something like this (static, 'cause you probably would want it in a utility class):

  public static Rectangle getScreenBounds(Component top){
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gd = ge.getScreenDevices();

    if (top != null){
        Rectangle bounds = top.getBounds();
        int centerX = (int) bounds.getCenterX();
        int centerY = (int) bounds.getCenterY();

        for (GraphicsDevice device : gd){
            GraphicsConfiguration gc = device.getDefaultConfiguration();
            Rectangle r = gc.getBounds();
            if (r.contains(centerX, centerY)){
                return r;
            }
        }
    }
    return gd[0].getDefaultConfiguration().getBounds();
}

public void centerWindowOnScreen(Window windowToCenter){
    Rectangle bounds = getScreenBounds(windowToCenter);
    Point newPt = new Point(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
    Dimension componentSize = windowToCenter.getSize();
    newPt.x -= componentSize.width / 2;
    newPt.y -= componentSize.height / 2;
    windowToCenter.setLocation(newPt);

}

As for default button, it's JDialog.getRootPane().setDefaultButton(btn), but button has to be already added to the dialog, and visible.

Garry answered 21/6, 2010 at 14:4 Comment(3)
I work with two displays here. One is even vertical shifted. camickr/Rehans answers works for me. It centers the window on the first screen. Isn't that what your method try's to accomplish? Or am I missing something here? Thanks!Vaginal
@Vaginal not really. My method is trying to find on which screen your component is shown, so you can center it on this screen, not only on the first.Garry
thanks for clarifying! Well thats pretty need! A shame this is not offered by default!Vaginal

© 2022 - 2024 — McMap. All rights reserved.