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

PointcenterPoint(int width, int height, JFrame frame)
center Point
Point point = null;
Dimension frameSize = frame.getSize();
Point framePoint = frame.getLocation();
point = new Point(framePoint.x + (frameSize.width / 2) - (width / 2),
        framePoint.y + (frameSize.height / 2) - (height / 2));
return point;
voidcenterPosition(JFrame invokerFrame, Window w, int width, int height)
center Position
int x = invokerFrame.getX() + (invokerFrame.getWidth() - width) / 2;
int y = invokerFrame.getY() + (invokerFrame.getHeight() - height) / 2;
w.setBounds(x, y, width, height);
voidcenterWindow(JFrame frame)
center Window
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screenSize = tk.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
frame.setLocation(screenWidth / 4, screenHeight / 4);
voidcenterWindow(JFrame frame)
center Window
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);
voidcenterWindow(JFrame frame)
Move frame to center of current screen
Point centerPoint = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
frame.setLocation(Math.max(0, centerPoint.x - (frame.getWidth() / 2)),
        Math.max(0, centerPoint.y - (frame.getHeight() / 2)));
voidcenterWindow(JFrame window)
center Window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = window.getSize();
if (frameSize.height > screenSize.height) {
    frameSize.height = screenSize.height;
if (frameSize.width > screenSize.width) {
    frameSize.width = screenSize.width;
window.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
voidcenterWindowInFrame(Component window, Window frame)
center Window In Frame
Dimension dimCenter;
Point ptOffset;
if (frame != null) {
    if ((!(frame instanceof Frame) || ((Frame) frame).getState() != Frame.ICONIFIED) && frame.isShowing()) {
        dimCenter = frame.getSize();
        ptOffset = frame.getLocation();
    } else {
        centerWindowInFrame(window, null);
...
voidcenterWindowOnParent(Window w, JFrame parent)
center Window On Parent
Rectangle bounds = parent.getBounds();
int cx = bounds.x + bounds.width / 2;
int cy = bounds.y + bounds.height / 2;
Dimension mySize = w.getSize();
w.setLocation(cx - mySize.width / 2, cy - mySize.height / 2);
voidcenterWithinDesktop(JInternalFrame frame)
Centers passed internal frame within its desktop area.
if (frame == null) {
    throw new IllegalArgumentException("null JInternalFrame passed");
final Container parent = frame.getDesktopPane();
if (parent != null && parent.isVisible()) {
    center(frame, new Rectangle(new Point(0, 0), parent.getSize()));
JFramecreateCenteredJFrame(String title, int width, int height, boolean autoclose)
creates a window with dimensions as component space
JFrame frame = new JFrame(title);
if (autoclose)
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width + 16, height + 38);
frame.setLocation((DESKTOP_SIZE.width - width + 16) / 2, (DESKTOP_SIZE.height - height + 38) / 2);
return frame;