Java JFrame Center centerFrameOnMainDisplay(final JFrame frame)

Here you can find the source of centerFrameOnMainDisplay(final JFrame frame)

Description

Center a frame on the main display

License

Open Source License

Parameter

Parameter Description
frame The frame to center

Declaration

public static void centerFrameOnMainDisplay(final JFrame frame) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;

import javax.swing.JFrame;

public class Main {
    /**// www. ja va2s  . c o  m
     * Center a frame on the main display
     *
     * @param frame
     *            The frame to center
     */
    public static void centerFrameOnMainDisplay(final JFrame frame) {
        final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        final GraphicsDevice[] screens = ge.getScreenDevices();
        if (screens.length < 1) {
            return; // Silently fail.
        }
        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);
        frame.setLocation(x, y);
    }
}

Related

  1. centerFrame(JFrame frame)
  2. centerFrame(JFrame frame)
  3. centerFrame(JFrame frame)
  4. centerFrame(JFrame frame, boolean isPopup)
  5. centerFrame(Window owner)
  6. centerFrameOnScreen(JFrame frame)
  7. centerFrameOnScreen(JFrame frame)
  8. centerFrameTo(final JFrame frameToCenter, final Dimension orientDim)
  9. centerJFrame(JFrame frame)