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

voidcenterFrame(Window owner)
center Frame
Dimension size = owner.getSize();
owner.setLocation(center(size, getViewableRectangle()));
voidcenterFrameOnMainDisplay(final JFrame frame)
Center a frame on the main display
final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
final GraphicsDevice[] screens = ge.getScreenDevices();
if (screens.length < 1) {
    return; 
final Rectangle screenBounds = screens[0].getDefaultConfiguration().getBounds();
final int x = (int) ((screenBounds.getWidth() - frame.getWidth()) / 2);
final int y = (int) ((screenBounds.getHeight() - frame.getHeight()) / 2);
...
voidcenterFrameOnScreen(JFrame frame)
center Frame On Screen
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
    frameSize.height = screenSize.height;
if (frameSize.width > screenSize.width) {
    frameSize.width = screenSize.width;
...
voidcenterFrameOnScreen(JFrame frame)
Centers a JFrame on screen.
final Dimension position = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((position.width - frame.getWidth()) / 2, (position.height - frame.getHeight()) / 2);
voidcenterFrameTo(final JFrame frameToCenter, final Dimension orientDim)
center Frame To
Dimension frameSize = frameToCenter.getSize();
int x = (orientDim.width - frameSize.width) / 2;
int y = (orientDim.height - frameSize.height) / 2;
frameToCenter.setLocation(x, y);
voidcenterJFrame(JFrame frame)
center J Frame
int width = frame.getWidth();
int height = frame.getHeight();
Dimension screen = getScreenSize();
double x = ((double) screen.width / 2) - ((double) width / 2d);
double y = ((double) screen.height / 2) - ((double) height / 2d);
x = Math.round(x);
y = Math.round(y);
frame.setLocation((int) x, (int) y);
...
voidcenterOnFrame(JDialog dialog, JFrame frame)
center On Frame
Point frameLoc = frame.getLocation();
Dimension frameSize = frame.getSize();
Dimension dialogSize = dialog.getSize();
int dialogX = frameLoc.x + (frameSize.width - dialogSize.width) / 2;
int dialogY = frameLoc.y + (frameSize.height - dialogSize.height) / 2;
dialog.setLocation(dialogX, dialogY);
voidcenterOnFrame(JFrame frame, Component comp)
center On Frame
Point p = frame.getLocation();
int left = p.x + (frame.getWidth() - comp.getWidth()) / 2;
int top = p.y + (frame.getHeight() - comp.getHeight()) / 2;
comp.setLocation(left, top);
voidcenterOnParent(JDialog child, JFrame parent)
Centers a Dialog on its parent frame;
int posX = parent.getX() + (parent.getWidth() / 2) - (child.getWidth() / 2);
int posY = parent.getY() + (parent.getHeight() / 2) - (child.getHeight() / 2);
child.setBounds(posX, posY, child.getWidth(), child.getHeight());
voidcenterOnScreen(JFrame frame, int screenID)
Centers a frame on screen.
Rectangle screenBounds = getScreenBounds(screenID);
int x = screenBounds.x + screenBounds.width / 2 - frame.getWidth() / 2;
int y = screenBounds.y + screenBounds.height / 2 - frame.getHeight() / 2;
frame.setLocation(x, y);