Java JFrame Center centerWithinDesktop(JInternalFrame frame)

Here you can find the source of centerWithinDesktop(JInternalFrame frame)

Description

Centers passed internal frame within its desktop area.

License

Open Source License

Parameter

Parameter Description
frame The internal frame to be centered.

Exception

Parameter Description
IllegalArgumentException If <TT>frame</TT> is <TT>null</TT>.

Declaration

public static void centerWithinDesktop(JInternalFrame frame) 

Method Source Code


//package com.java2s;
/*/*from   w w w.  ja v a  2 s.c om*/
 * Copyright (C) 2001-2004 Colin Bell
 * colbell@users.sourceforge.net
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import javax.swing.*;

import java.awt.*;

public class Main {
    /**
     * Centers passed internal frame within its desktop area. If centering
     * would cause the title bar to go off the top of the screen then move the
     * window down.
     *
     * @param   frame   The internal frame to be centered.
     *
     * @throws IllegalArgumentException    If <TT>frame</TT> is <TT>null</TT>.
     */
    public static void centerWithinDesktop(JInternalFrame frame) {
        if (frame == null) {
            throw new IllegalArgumentException("null JInternalFrame passed");
        }
        final Container parent = frame.getDesktopPane();
        if (parent != null && parent.isVisible()) {
            center(frame, new Rectangle(new Point(0, 0), parent.getSize()));
        }
    }

    /**
     * Centers <CODE>wind</CODE> within the passed rectangle.
     *
     * @param   wind   The Window to be centered.
     * @param   rect   The rectangle (in screen coords) to center
     *               <CODE>wind</CODE> within.
     *
     * @throws   IllegalArgumentException
     *         If <TT>Window</TT> or <TT>Rectangle</TT> is <TT>null</TT>.
     */
    private static void center(Component wind, Rectangle rect) {
        if (wind == null || rect == null) {
            throw new IllegalArgumentException("null Window or Rectangle passed");
        }
        Dimension windSize = wind.getSize();
        int x = ((rect.width - windSize.width) / 2) + rect.x;
        int y = ((rect.height - windSize.height) / 2) + rect.y;
        if (y < rect.y) {
            y = rect.y;
        }
        wind.setLocation(x, y);
    }
}

Related

  1. centerWindow(JFrame frame)
  2. centerWindow(JFrame frame)
  3. centerWindow(JFrame window)
  4. centerWindowInFrame(Component window, Window frame)
  5. centerWindowOnParent(Window w, JFrame parent)
  6. createCenteredJFrame(String title, int width, int height, boolean autoclose)
  7. getCenteringFrame()
  8. getCenterOfTheWindow(JFrame frame)
  9. placeInCenter(JFrame frame)