JFrame positioning in the center of the window on MAC OSX

Any body please can help me how to focus on JFrame

on Mac. OS X?

I tried:

this.setLocationRelativeto(null);

this.setLocationRelativeto(this);
this.setLocationRelativeto(getRootPane());

      

.. and

    final Toolkit toolkit = Toolkit.getDefaultToolkit();
    final Dimension screenSize = toolkit.getScreenSize();
    final int x = (screenSize.width - this.getWidth()) / 2;
    final int y = (screenSize.height - this.getHeight()) / 2;
    this.setLocation(x, y);

      

None of this was done, my frame is still at the bottom and hidden behind the Mac dock.

+3


source to share


1 answer


The location should be set after you've packed your frame (which calculates the frame size). And after that it should be visible (it is hidden by default otherwize).



 pack();
 setLocationRelativeTo(null);
 setVisible(true);

      

+8


source







All Articles