Java JFrame Show showDemoFrame(JFrame frame)

Here you can find the source of showDemoFrame(JFrame frame)

Description

Method for demo purposes.

License

Open Source License

Parameter

Parameter Description
frame Frame to show

Declaration

public static void showDemoFrame(JFrame frame) 

Method Source Code

//package com.java2s;
/*/*from w w  w . j av a 2  s .com*/
 *   Swing Explorer. Tool for developers exploring Java/Swing-based application internals. 
 *     Copyright (C) 2012, Maxim Zakharenkov
 *
 *   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 java.awt.Component;

import java.awt.Dimension;

import java.awt.Point;
import java.awt.Toolkit;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

public class Main {
    /**
     * Method for demo purposes.
     * Shows specified frame with 1/1.5 size
     * of screen. When frame is closed the System.exit(0) is executed;
     * @param frame Frame to show
     */
    public static void showDemoFrame(JFrame frame) {
        // Making good size and center the frame
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setSize((int) (screenSize.width / 1.5), (int) (screenSize.height / 1.5));
        center(frame);

        // Adding exit listener
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.setVisible(true);
    }

    /**
     * Centers specified child component on the specified container.
     * @param parent Container used to center on, if <code>null</code>
     * then child is centered relatively to the whole screen.
     * @param child The component to be centered.
     */
    public static void center(Component parent, Component child) {
        if (parent == null) {
            center(child);
        } else {
            center(parent.getLocationOnScreen(), parent.getSize(), child, false);
        }
    }

    /**
     * Centers specified component on a screen.
     * @param component The component to be centered.
     */
    public static void center(Component component) {
        // size will be restricted against screen size anyway
        // that's why boolean argument is false.
        // anyway this argument should not be removed - it is simply reserved
        // for future if MDI restriction will be required.
        center(new Point(0, 0), null, component, true);

    }

    private static void center(Point parentLocation, Dimension parentSize, Component component,
            boolean restrictSize) {

        // this dimension is used to fit into screen after positioning performed
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        if (parentSize == null) {
            parentSize = screenSize;
        }

        Dimension childSize = component.getSize();

        // Disallows child GUI component have greater size than parent GUI
        // container have.
        // This method is reserved to restrict area when MDI component used.
        if (restrictSize) {
            if (childSize.height > parentSize.height) {
                childSize.height = parentSize.height;
            }
            if (childSize.width > parentSize.width) {
                childSize.width = parentSize.width;
            }
        }

        // check child size against screen size anyway:
        if (childSize.height > screenSize.height) {
            childSize.height = screenSize.height;
        }
        if (childSize.width > screenSize.width) {
            childSize.width = screenSize.width;
        }
        component.setSize(childSize.width, childSize.height);

        int childLocationX = (parentSize.width - childSize.width) / 2 + parentLocation.x;
        int childLocationY = (parentSize.height - childSize.height) / 2 + parentLocation.y;

        // check location and size to fit into screen:
        if (childSize.width + childLocationX > screenSize.width) {
            childLocationX = screenSize.width - childSize.width;
        }
        if (childSize.height + childLocationY > screenSize.height) {
            childLocationY = screenSize.height - childSize.height;
        }
        if (childLocationX < 0) {
            childLocationX = 0;
        }
        if (childLocationY < 0) {
            childLocationY = 0;
        }

        // Centers child GUI component on the parent GUI container.
        component.setLocation(childLocationX, childLocationY);
    }
}

Related

  1. isShowing(JFrame f)
  2. open(JFrame view)
  3. show(JFrame frame)
  4. showAndSaveReportFile(JFrame frmParent, File defaultReportFile, String data)
  5. showFrame(JFrame frame)
  6. showFrame(JFrame frame, JFrame parent)
  7. showHelp(JFrame frame, String path)
  8. showModalFrame(final JFrame newFrame, final JFrame owner)