Java Window Center positionCenterScreen(Window window)

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

Description

position Center Screen

License

Apache License

Declaration

public static void positionCenterScreen(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 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;/*  w w w  .  j a va2  s. c  o  m*/
        }
        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. centerOnParent(final Window child, final boolean absolute)
  2. centerPosition(Window window, Window w, int width, int height)
  3. centerWindow(Window w)
  4. packAndCenterWindow(Window dlg)
  5. packAndDisplayInCenterOfScreen(final Window f)
  6. showCentered(Window window)
  7. showCenterWindow(Window parent, Window window)