Java JFrame Center centerWindow(JFrame frame)

Here you can find the source of centerWindow(JFrame frame)

Description

center Window

License

Open Source License

Declaration

public static void centerWindow(JFrame frame) 

Method Source Code


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

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JDialog;

public class Main {
    public static void centerWindow(JFrame frame) {
        // Center the window
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = frame.getSize();
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }/*from   www.j a  v  a2  s. com*/
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    }

    public static void centerWindow(JDialog dialog) {
        // Center the window
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension dialogSize = dialog.getSize();
        if (dialogSize.height > screenSize.height) {
            dialogSize.height = screenSize.height;
        }
        if (dialogSize.width > screenSize.width) {
            dialogSize.width = screenSize.width;
        }
        dialog.setLocation((screenSize.width - dialogSize.width) / 2, (screenSize.height - dialogSize.height) / 2);
    }
}

Related

  1. centerOnFrame(JFrame frame, Component comp)
  2. centerOnParent(JDialog child, JFrame parent)
  3. centerOnScreen(JFrame frame, int screenID)
  4. centerPoint(int width, int height, JFrame frame)
  5. centerPosition(JFrame invokerFrame, Window w, int width, int height)
  6. centerWindow(JFrame frame)
  7. centerWindow(JFrame frame)
  8. centerWindow(JFrame window)
  9. centerWindowInFrame(Component window, Window frame)