Java JComponent Size findCenterBounds(Dimension componentSize)

Here you can find the source of findCenterBounds(Dimension componentSize)

Description

Helps client code place components on the center of the screen.

License

Sun Public License Notice

Parameter

Parameter Description
componentSize the size of the component

Return

bounds of the centered component

Declaration

public static Rectangle findCenterBounds(Dimension componentSize) 

Method Source Code


//package com.java2s;
/*//  w ww .  j a v a 2s .  c om
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

import java.awt.*;

public class Main {
    /**
     * Helps client code place components on the center of the screen.  It
     * handles multiple monitor configuration correctly
     *
     * @param componentSize the size of the component
     * @return bounds of the centered component
     *
     * @since 2.5
     */
    public static Rectangle findCenterBounds(Dimension componentSize) {
        return findCenterBounds(getCurrentGraphicsConfiguration(), componentSize);
    }

    /**
     * Helps client code place components on the center of the screen.  It
     * handles multiple monitor configuration correctly
     *
     * @param gconf the GraphicsConfiguration of the monitor
     * @param componentSize the size of the component
     * @return bounds of the centered component
     */
    private static Rectangle findCenterBounds(GraphicsConfiguration gconf, Dimension componentSize) {
        if (gconf == null)
            gconf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
                    .getDefaultConfiguration();

        Rectangle bounds = gconf.getBounds();
        return new Rectangle(bounds.x + (bounds.width - componentSize.width) / 2,
                bounds.y + (bounds.height - componentSize.height) / 2, componentSize.width, componentSize.height);
    }

    /**
     * Finds out the monitor where the user currently has the input focus.
     * This method is usually used to help the client code to figure out on
     * which monitor it should place newly created windows/frames/dialogs.
     *
     * @return the GraphicsConfiguration of the monitor which currently has the
     * input focus
     */
    private static GraphicsConfiguration getCurrentGraphicsConfiguration() {
        Frame[] frames = Frame.getFrames();

        for (int i = 0; i < frames.length; i++) {
            if (javax.swing.SwingUtilities.findFocusOwner(frames[i]) != null) {
                return frames[i].getGraphicsConfiguration();
            }
        }
        return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
    }
}

Related

  1. doSetSizeInEDT(final Component component, final int width, final int height)
  2. emphasizeToolTip()
  3. equalizeSize(final JComponent... components)
  4. equalizeSizes(JComponent[] components)
  5. evenSizes(JComponent[] components)
  6. fixSize(JComponent c, Dimension d)
  7. fixSize(JComponent... items)
  8. fixSize(T comp, int width)
  9. forceSize(JComponent component, int width, int height)