Java Window Center showCentered(Window window)

Here you can find the source of showCentered(Window window)

Description

Shows the window centered on its parent window, or the screen if there is no parent window.

License

Apache License

Declaration

public static void showCentered(Window window) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import javax.swing.*;

import java.awt.*;

public class Main {
    public static void showCentered(JPanel panel) {
        JFrame frame = new JFrame();
        frame.setContentPane(panel);/* w  w  w.ja v a  2 s  .  c  om*/
        frame.pack();
        center(frame);
        frame.setVisible(true);
    }

    /**
     * Shows the window centered on its parent window, or the screen if there is no parent window.
     */
    public static void showCentered(Window window) {
        center(window);
        window.setVisible(true);
    }

    /**
     * Centers the window on its parent window, or the screen if there is no parent window.
     */
    public static void center(Window window) {
        Point origin;
        Dimension parentSize;

        Dimension maxSize = getMaxSize(window);
        Container parent = window.getParent();
        if ((parent != null) && parent.isShowing()) {
            origin = parent.getLocationOnScreen();
            parentSize = parent.getSize();
        } else {
            Insets screenInsets = getScreenInsets(window);
            origin = new Point(screenInsets.left, screenInsets.top);
            parentSize = maxSize;
        }

        Dimension windowSize = new Dimension(window.getSize());
        if (windowSize.width > maxSize.width) {
            windowSize.width = maxSize.width;
        }
        if (windowSize.height > maxSize.height) {
            windowSize.height = maxSize.height;
        }
        window.setBounds(origin.x + parentSize.width / 2 - windowSize.width / 2,
                origin.y + parentSize.height / 2 - windowSize.height / 2, windowSize.width, windowSize.height);
    }

    public static Dimension getMaxSize(Window frame) {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        GraphicsConfiguration config = frame.getGraphicsConfiguration();
        Dimension availableScreenSize = new Dimension(toolkit.getScreenSize());
        Insets insets = toolkit.getScreenInsets(config);
        availableScreenSize.width -= (insets.left + insets.right);
        availableScreenSize.height -= (insets.top + insets.bottom);
        return availableScreenSize;
    }

    private static Insets getScreenInsets(Window window) {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        GraphicsConfiguration config = window.getGraphicsConfiguration();
        return toolkit.getScreenInsets(config);
    }
}

Related

  1. centerPosition(Window window, Window w, int width, int height)
  2. centerWindow(Window w)
  3. packAndCenterWindow(Window dlg)
  4. packAndDisplayInCenterOfScreen(final Window f)
  5. positionCenterScreen(Window window)
  6. showCenterWindow(Window parent, Window window)