Java Window Center showCenterWindow(Window parent, Window window)

Here you can find the source of showCenterWindow(Window parent, Window window)

Description

show Center Window

License

Apache License

Declaration

public static void showCenterWindow(Window parent, 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 showCenterWindow(Window parent, Window window) {
        positionCenterWindow(parent, window);
        window.setVisible(true);//from   w w w.  j  a v  a 2s .  c  o  m
    }

    public static void positionCenterWindow(Window parent, Window window) {
        if (parent == null) {
            positionCenterScreen(window);
            return;
        } else {
            if (!parent.isVisible()) {
                positionCenterScreen(window);
                return;
            }
        }
        Dimension sSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension wSize = window.getSize();
        if (wSize.height > sSize.height) {
            wSize.height = sSize.height;
        }
        if (wSize.width > sSize.width) {
            wSize.width = sSize.width;
        }
        window.setSize(wSize);
        Rectangle pBounds = parent.getBounds();
        int x = pBounds.x + ((pBounds.width - wSize.width) / 2);
        if (x < 0) {
            x = 0;
        }
        int y = pBounds.y + ((pBounds.height - wSize.height) / 2);
        if (y < 0) {
            y = 0;
        }
        if (x + wSize.width > sSize.width) {
            x = sSize.width - wSize.width;
        }
        if (y + wSize.height > sSize.height) {
            y = sSize.height - wSize.height;
        }
        window.setLocation(x, y);
        window.toFront();
    }

    public static void positionCenterScreen(Window window) {
        Dimension maxDimension = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension wSize = window.getSize();
        int maxWidth = maxDimension.width;
        int maxHeight = maxDimension.height;
        // fit on window
        if (wSize.height > maxHeight) {
            wSize.height = maxHeight;
        }
        if (wSize.width > maxWidth) {
            wSize.width = maxWidth;
        }
        window.setSize(wSize);
        if (maxDimension.width != wSize.width && maxDimension.height != wSize.height) {
            int x = (maxDimension.width - wSize.width) / 2;
            int y = (maxDimension.height - wSize.height) / 2;
            window.setLocation(x, y);
        } else {
            if (window instanceof JFrame) {
                JFrame f = (JFrame) window;
                f.setExtendedState(JFrame.MAXIMIZED_BOTH);
            } else {
                window.setLocation(0, 0);
            }
        }
        window.toFront();
    }
}

Related

  1. centerWindow(Window w)
  2. packAndCenterWindow(Window dlg)
  3. packAndDisplayInCenterOfScreen(final Window f)
  4. positionCenterScreen(Window window)
  5. showCentered(Window window)