Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centering a jFrame

Tags:

java

swing

jframe

I'm trying to locate my frames at the center of the screen , I know that this code must work well:

Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((screen.getWidth() - getWidth()) /2);
int y = (int) ((screen.getHeight() -getHeight()) /2);
setLocation(x, y); 

or this one :

Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screenSize = tk.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
setSize(screenWidth / 2, screenHeight / 2);
setLocation(screenWidth / 4, screenHeight / 4);

I have tried all the possible codes but I don't know why they don't work, the just move the frame to the bottom right corner of the screen I have used setRelative to null but again it doesn't work.

I could finally bring the frame very close to the center by multiplying the width by 5 and dividing it by something but I know that this is not the right way.

could anyone please explain to me what could be wrong ?

Problem Fixed :

solution: I found the best way myself , if you ever come up with the same problem simply let the netbeans do that automatically by going to properties and checking generate center , forget about the stupid codes.

like image 581
Loop Masters Avatar asked Jun 27 '12 17:06

Loop Masters


People also ask

How do I change the position of a JFrame?

To change the position of JFrame on the screen, JFrame provides the method JFrame. setlocation(int x, int y), you need two parameters 'x' represents the position of the x axis and 'y' represents the position of the y axis. The upper left corner of your screen is (0,0). If you give NULL as parameter to JFrame.

How do I center a JFrame in Netbeans?

Within the Netbeans Designer area, choose your JFrame, go to code. Then choose the Generate Center Option. Save this answer.

What is setLocationRelativeTo java?

setLocationRelativeTo(null); As the Javadoc for the setLocationRelativeTo method states: Sets the location of the window relative to the specified component. If the component is not currently showing, or c (the Component) is null, the window is centered on the screen.


2 Answers

The easiest way to center frame on the main screen (there are options if you have multiply screens) is to use setLocationRelativeTo(...) frame method with null as argument:

JFrame frame = new JFrame ();
frame.setSize ( 500, 300 );
frame.setLocationRelativeTo ( null );
frame.setVisible ( true );

In case that doesn't work - try this one (direct coordinates):

Dimension ss = Toolkit.getDefaultToolkit ().getScreenSize ();
Dimension frameSize = new Dimension ( 500, 300 );

JFrame frame = new JFrame ();
frame.setBounds ( ss.width / 2 - frameSize.width / 2, 
                  ss.height / 2 - frameSize.height / 2,
                  frameSize.width, frameSize.height );
frame.setVisible ( true );

Last and "ultimate" example - it will display centered frame @ each available system screen:

public static void main ( String[] args )
{
    Dimension frameSize = new Dimension ( 500, 300 );
    for ( GraphicsDevice screen : getGraphicsDevices () )
    {
        GraphicsConfiguration gc = screen.getDefaultConfiguration ();
        Rectangle sb = gc.getBounds ();

        JFrame frame = new JFrame ( gc );
        frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
        frame.setBounds ( sb.x + sb.width / 2 - frameSize.width / 2,
                sb.y + sb.height / 2 - frameSize.height / 2, frameSize.width,
                frameSize.height );
        frame.setVisible ( true );
    }
}

public static List<GraphicsDevice> getGraphicsDevices ()
{
    List<GraphicsDevice> devices = new ArrayList<GraphicsDevice> ();
    for ( GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment ()
            .getScreenDevices () )
    {
        if ( gd.getType () == GraphicsDevice.TYPE_RASTER_SCREEN )
        {
            if ( gd == GraphicsEnvironment.getLocalGraphicsEnvironment ()
                    .getDefaultScreenDevice () )
            {
                devices.add ( 0, gd );
            }
            else
            {
                devices.add ( gd );
            }
        }
    }
    return devices;
}
like image 83
Mikle Garin Avatar answered Sep 19 '22 21:09

Mikle Garin


It's enough if you use

setLocationRelativeTo(null);

According to Javadoc for java.awt.Window.setLocationRelativeTo(),

If the component is null, ... the window is placed in the center of the screen.

like image 23
Honza Zidek Avatar answered Sep 18 '22 21:09

Honza Zidek