Java Swing BoxLayout show(JComponent component)

Here you can find the source of show(JComponent component)

Description

show

License

Apache License

Declaration

public static void show(JComponent component) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import javax.swing.*;

import java.awt.*;

public class Main {
    public static void show(JComponent component) {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(component, BorderLayout.CENTER);
        showCentered(panel);//  www  .j  ava  2 s  .c om
    }

    public static void showCentered(JPanel panel) {
        JFrame frame = new JFrame();
        frame.setContentPane(panel);
        frame.pack();
        center(frame);
        frame.setVisible(true);
    }

    /**
     * Shows the window centered on its parent window, or the screen if there is no parent window.
     */
    public static void showCentered(Window window) {
        center(window);
        window.setVisible(true);
    }

    /**
     * Centers the window on its parent window, or the screen if there is no parent window.
     */
    public static void center(Window window) {
        Point origin;
        Dimension parentSize;

        Dimension maxSize = getMaxSize(window);
        Container parent = window.getParent();
        if ((parent != null) && parent.isShowing()) {
            origin = parent.getLocationOnScreen();
            parentSize = parent.getSize();
        } else {
            Insets screenInsets = getScreenInsets(window);
            origin = new Point(screenInsets.left, screenInsets.top);
            parentSize = maxSize;
        }

        Dimension windowSize = new Dimension(window.getSize());
        if (windowSize.width > maxSize.width) {
            windowSize.width = maxSize.width;
        }
        if (windowSize.height > maxSize.height) {
            windowSize.height = maxSize.height;
        }
        window.setBounds(origin.x + parentSize.width / 2 - windowSize.width / 2,
                origin.y + parentSize.height / 2 - windowSize.height / 2, windowSize.width, windowSize.height);
    }

    public static Dimension getMaxSize(Window frame) {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        GraphicsConfiguration config = frame.getGraphicsConfiguration();
        Dimension availableScreenSize = new Dimension(toolkit.getScreenSize());
        Insets insets = toolkit.getScreenInsets(config);
        availableScreenSize.width -= (insets.left + insets.right);
        availableScreenSize.height -= (insets.top + insets.bottom);
        return availableScreenSize;
    }

    private static Insets getScreenInsets(Window window) {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        GraphicsConfiguration config = window.getGraphicsConfiguration();
        return toolkit.getScreenInsets(config);
    }
}

Related

  1. hbox(Component[] components, int spacing)
  2. makeVerticalBoxPanel(int margin)
  3. newBox(String name, String... items)
  4. newBoxForComponent(Component c)
  5. show(JComponent comp, String windowName)
  6. vbox(Component c1, Component c2)
  7. xBoxLayoutPanel(Component... components)