Java Window Center packAndDisplayInCenterOfScreen(final Window f)

Here you can find the source of packAndDisplayInCenterOfScreen(final Window f)

Description

Packs a Window and display it in center of the screen.

License

Apache License

Parameter

Parameter Description
f the window to pack and display

Declaration

public static void packAndDisplayInCenterOfScreen(final Window f) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (C) 2009, 2015, Danilo Pianini and contributors
 * listed in the project's build.gradle or pom.xml file.
 *
 * This file is distributed under the terms of the Apache License, version 2.0
 *******************************************************************************/

import java.awt.Component;
import java.awt.Dimension;

import java.awt.Toolkit;
import java.awt.Window;

import javax.swing.SwingUtilities;

public class Main {
    /**//  w w  w  .  ja v  a2 s. co  m
     * Packs a Window and display it in center of the screen.
     * 
     * @param f
     *            the window to pack and display
     */
    public static void packAndDisplayInCenterOfScreen(final Window f) {
        f.pack();
        displayInCenterOfScreen(f);
    }

    /**
     * Displays a Component in center of the screen.
     * 
     * @param f
     *            the component to display
     */
    public static void displayInCenterOfScreen(final Component f) {
        final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                f.setLocation((int) (screenSize.getWidth() - f.getWidth()) / 2,
                        (int) (screenSize.getHeight() - f.getHeight()) / 2);
                f.setVisible(true);
            }
        });
    }
}

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. positionCenterScreen(Window window)
  6. showCentered(Window window)
  7. showCenterWindow(Window parent, Window window)