Java Utililty Methods JFrame Center

List of utility methods to do JFrame Center

Description

The list of methods to do JFrame Center are organized into topic(s).

Method

FramegetCenteringFrame()
get Centering Frame
for (Container c = COMPONENT; c != null; c = c.getParent()) {
    if (c instanceof Frame) {
        return (Frame) c;
return null;
RectanglegetCenterOfTheWindow(JFrame frame)
get Center Of The Window
Dimension d = frame.getSize();
double x = d.getWidth() / 2.0 - 150;
double y = d.getHeight() / 2.0 - 50;
Rectangle r = new Rectangle((int) x, (int) y, 0, 0);
return r;
voidplaceInCenter(JFrame frame)
place In Center
GraphicsConfiguration gc = frame.getGraphicsConfiguration();
int screenHeight = (int) gc.getBounds().getHeight();
int screenWidth = (int) gc.getBounds().getWidth();
int windowWidth = frame.getWidth();
int windowHeight = frame.getHeight();
frame.setLocation((screenWidth - windowWidth) / 2, (screenHeight - windowHeight) / 2);
voidposiToScreenCenter(JFrame frame)
Set frame to location screen center.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height)
    frameSize.height = screenSize.height;
if (frameSize.width > screenSize.width)
    frameSize.width = screenSize.width;
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
voidputWindowInCenter(JFrame window)
put Window In Center
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screenSize = tk.getScreenSize();
window.pack();
int leftCoordinate = (screenSize.width / 2) - (window.getWidth() / 2);
int topCoordinate = (screenSize.height / 2) - (window.getHeight() / 2);
window.setLocation(leftCoordinate, topCoordinate);
voidSET_FRAME_CENTER(JFrame frame)
SEFRAMCENTER
Dimension screenSize = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
Dimension windowSize = new Dimension(frame.getPreferredSize());
int wdwLeft = screenSize.width / 2 - windowSize.width / 2;
int wdwTop = screenSize.height / 2 - windowSize.height / 2;
frame.pack();
frame.setLocation(wdwLeft, wdwTop);
voidsizeAndCenterFrame(JFrame aFrame, int aWidth, int aHeight)
sizes the JFrame
aFrame.setSize(aWidth, aHeight);
aFrame.setLocationRelativeTo(null);