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

voidcenterDialogOnFrame(JFrame parentFrame, JDialog dialog)
Center the dialog on the parent frame.
Point topLeft = parentFrame.getLocationOnScreen();
Dimension parentSize = parentFrame.getSize();
Dimension dialogSize = dialog.getSize();
int x;
int y;
if (parentSize.width > dialogSize.width) {
    x = ((parentSize.width - dialogSize.width) / 2) + topLeft.x;
} else {
...
voidcenterFrame(final JFrame target)
Centers JFrame on screen
final Rectangle screen = getScreenRect();
final Rectangle frameSize = target.getBounds();
final int x = (screen.width - frameSize.width) / 2;
final int y = (screen.height - frameSize.height) / 2;
target.setLocation(x, y);
voidcenterFrame(JFrame frame)
center Frame
Dimension frameSize = frame.getSize();
frame.setLocation(what(frameSize.width, screenSize.width), what(frameSize.height, screenSize.height));
frame.setVisible(true);
voidcenterFrame(JFrame frame)
center Frame
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
frame.setLocation((screenSize.width / 2) - (frameSize.width / 2),
        (screenSize.height / 2) - (frameSize.height / 2));
voidcenterFrame(JFrame frame)
Centers the frame on the screen and sets its bounds.
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Point center = ge.getCenterPoint();
Rectangle bounds = ge.getMaximumWindowBounds();
int w = Math.max(bounds.width / 2, Math.min(frame.getWidth(), bounds.width));
int h = Math.max(bounds.height / 2, Math.min(frame.getHeight(), bounds.height));
int x = center.x - w / 2, y = center.y - h / 2;
frame.setBounds(x, y, w, h);
if ((w == bounds.width) && (h == bounds.height)) {
...
voidcenterFrame(JFrame frame)
Centers a JFrame on the user's screen.
frame.setLocationRelativeTo(null);
voidcenterFrame(JFrame frame)
Centers a frame on the screen
if (frame == null) {
    throw new NullPointerException("The frame can't be null");
final Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
final Dimension f = frame.getSize();
frame.setLocation((s.width - f.width) / 2, (s.height - f.height) / 2);
voidcenterFrame(JFrame frame)
center Frame
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int w = frame.getSize().width;
int h = frame.getSize().height;
int x = (dim.width - w) / 2;
int y = (dim.height - h) / 2;
frame.setLocation(x, y);
voidcenterFrame(JFrame frame)
Centers the given frame on the screen
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((dim.width - frame.getWidth()) / 2, (dim.height - frame.getHeight()) / 2);
voidcenterFrame(JFrame frame, boolean isPopup)
Centers a JFrame to the screen.
final Rectangle screenSize = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
        .getDefaultConfiguration().getBounds();
if (isPopup) {
    frame.setSize(screenSize.width / 2, screenSize.height / 2);
final Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
    frameSize.height = screenSize.height;
...