Example usage for java.awt Window pack

List of usage examples for java.awt Window pack

Introduction

In this page you can find the example usage for java.awt Window pack.

Prototype

@SuppressWarnings("deprecation")
public void pack() 

Source Link

Document

Causes this Window to be sized to fit the preferred size and layouts of its subcomponents.

Usage

From source file:SwingUtils.java

public static void centerOnScreen(Window window, boolean packFrame) {
    //Validate frames that have preset sizes
    //Pack frames that have useful preferred size info, e.g. from their layout
    if (packFrame) {
        window.pack();
    } else {/*w  ww . j av a 2s .com*/
        window.validate();
    }

    //Center the frame window
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = window.getSize();

    if (frameSize.height > screenSize.height) {
        frameSize.height = screenSize.height;
    }

    if (frameSize.width > screenSize.width) {
        frameSize.width = screenSize.width;
    }

    window.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
}

From source file:Main.java

/**
 * Center window on screen.//from   w w  w .ja v  a2s  . co  m
 *
 * @param window    window to be centered.
 * @param packFrame if <code>true</code> call window's <code>pack()</code>
 *                  method before centering.
 */
public static void centerOnScreen(Window window, boolean packFrame) {
    //Validate frames that have preset sizes
    //Pack frames that have useful preferred size info, e.g. from their layout
    if (packFrame) {
        window.pack();
    } else {
        window.validate();
    }

    //Center the frame window
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = window.getSize();

    if (frameSize.height > screenSize.height) {
        frameSize.height = screenSize.height;
    }

    if (frameSize.width > screenSize.width) {
        frameSize.width = screenSize.width;
    }

    window.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
}

From source file:AWTUtilities.java

/**
 * Packs and centers the given window relative to the given component. The
 * specified component may be <code>null</code>, in which case the window will
 * be centered on the screen. The method also makes sure that the target
 * window is fully visible by calling <code>forceToScreen</code>.
 *///from w  ww  . j  av  a  2s .c o m

public static void centerWindow(Window target, Component parent) {
    target.pack();

    Dimension size = target.getSize();
    Rectangle parentBounds = parent == null || !parent.isShowing() ? getUsableScreenBounds()
            : new Rectangle(parent.getLocationOnScreen(), parent.getSize());

    target.setLocation(parentBounds.x + (parentBounds.width - size.width) / 2,
            parentBounds.y + (parentBounds.height - size.height) / 2);

    forceToScreen(target);
}

From source file:Main.java

public static Window showCentered(Component parent, Window window) {
    if (null == window)
        throw new IllegalArgumentException("window null");

    if (window instanceof Dialog) {
        Dialog dialog = (Dialog) window;

        boolean isResizable = dialog.isResizable();

        dialog.setResizable(true);//from w ww  .j  a  v a 2  s  .c  o m
        dialog.pack();
        dialog.setResizable(isResizable);
    } else {
        window.pack();
    }

    Dimension windowSize = window.getPreferredSize();

    int newX;
    int newY;

    if (null == parent) {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

        newX = (screen.width - windowSize.width) / 2;
        newY = (screen.height - windowSize.height) / 2;
    } else {
        Dimension parentSize = parent.getSize();
        Point loc = parent.getLocation();

        newX = (parentSize.width - windowSize.width) / 2 + loc.x;
        newY = (parentSize.height - windowSize.height) / 2 + loc.y;

        if (0 > newX)
            newX = 0;
        if (0 > newY)
            newY = 0;
    }

    window.setLocation(newX, newY);
    window.setVisible(true);

    return window;
}

From source file:Main.java

public static void center(Window window, double ratio) {
    Rectangle bounds = window.getGraphicsConfiguration().getBounds();
    int width = bounds.width / 2;
    int height = bounds.height / 2;
    int centerX = (int) (bounds.x + bounds.width * ratio);
    int centerY = (int) (bounds.y + bounds.height * ratio);
    window.setLocation(centerX - width / 2, centerY - height / 2);
    window.setPreferredSize(new Dimension(width, height));
    window.pack();
}

From source file:Main.java

public static Window showBelow(Component parent, Window window, int horizontalAlignment) {
    final int DISTANCE = 2;

    if (null == parent)
        throw new IllegalArgumentException("parent null");
    if (null == window)
        throw new IllegalArgumentException("parent null");
    if (!((SwingConstants.LEADING == horizontalAlignment) || (SwingConstants.TRAILING == horizontalAlignment)
            || (SwingConstants.CENTER == horizontalAlignment)
            || (SwingConstants.SOUTH == horizontalAlignment))) {

        throw new IllegalArgumentException("Illegal horizontal alignment " + horizontalAlignment
                + " should be either SwingConstants.LEADING or "
                + "SwingConstants.TRAILING or SwingConstants.CENTER");
    }/*from  ww w . j a v a 2 s . c  o m*/

    window.pack();

    Dimension windowSize = window.getPreferredSize();
    Dimension parentSize = parent.getSize();

    Point loc = parent.getLocationOnScreen();

    int newX;

    if ((SwingConstants.CENTER == horizontalAlignment) || (SwingConstants.SOUTH == horizontalAlignment)) {

        newX = (parentSize.width - windowSize.width) / 2 + loc.x;
    } else if (SwingConstants.TRAILING == horizontalAlignment) {
        newX = loc.x + parentSize.width - windowSize.width;
    } else {
        newX = loc.x;
    }

    window.setLocation(newX, (loc.y + parentSize.height + DISTANCE));

    window.setVisible(true);

    return window;
}

From source file:Main.java

/**
 * Sets up the given window content pane by setting its border to provide a
 * good default spacer, and setting the content pane to the given components.
 * //  w  w  w.  ja  v  a 2 s  .c  om
 * @param aWindow
 *          the window to setup, cannot be <code>null</code>;
 * @param aCenterComponent
 *          the component that should appear at the center of the dialog;
 * @param aButtonPane
 *          the component that should appear at the bottom of the dialog
 * @param defaultButton
 *          the default button for this dialog; can be null for "none".
 * @see javax.swing.JRootPane#setDefaultButton(javax.swing.JButton)
 */
public static void setupWindowContentPane(final Window aWindow, final Component aCenterComponent,
        final Component aButtonPane, final JButton defaultButton) {
    final JPanel contentPane = new JPanel(new BorderLayout());
    contentPane.setBorder(BorderFactory.createEmptyBorder(DIALOG_PADDING, DIALOG_PADDING, //
            DIALOG_PADDING, DIALOG_PADDING));

    contentPane.add(aCenterComponent, BorderLayout.CENTER);
    contentPane.add(aButtonPane, BorderLayout.PAGE_END);

    if (aWindow instanceof JDialog) {
        ((JDialog) aWindow).setContentPane(contentPane);
        ((JDialog) aWindow).getRootPane().setDefaultButton(defaultButton);
    } else if (aWindow instanceof JFrame) {
        ((JFrame) aWindow).setContentPane(contentPane);
        ((JFrame) aWindow).getRootPane().setDefaultButton(defaultButton);
    }
    aWindow.pack();
}

From source file:org.uncommons.watchmaker.swing.evolutionmonitor.EvolutionMonitor.java

/**
 * Helper method for showing the evolution monitor in a frame or dialog.
 * @param newWindow The frame or dialog used to show the evolution monitor.
 *//*from ww w .  j a v  a2  s.  co  m*/
private void showWindow(Window newWindow) {
    if (window != null) {
        window.remove(getGUIComponent());
        window.setVisible(false);
        window.dispose();
        window = null;
    }
    newWindow.add(getGUIComponent(), BorderLayout.CENTER);
    newWindow.pack();
    newWindow.setVisible(true);
    this.window = newWindow;
}

From source file:com.alvermont.terraj.util.ui.LookAndFeelUtils.java

/**
 * Change the look and feel to either the native one or the cross
 * platform Java one//from w w w  .  j  a  v  a2s.  c  om
 *
 * @param wantSystem If <pre>true</pre> then we are selecting the native
 * look and feel
 * @param topLevel The top level component for the main frame
 * @return <pre>true</pre> if the look and feel was successfully changed
 */
public boolean setSystemLookAndFeel(boolean wantSystem, Window topLevel) {
    boolean ok = true;

    try {
        if (wantSystem) {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } else {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        }

        SwingUtilities.updateComponentTreeUI(topLevel);
        topLevel.pack();

        final LookAndFeelChangedEvent event = new LookAndFeelChangedEvent(this);

        fireLookAndFeelChangedEventListenerHandleLookAndFeelChangedEvent(event);
    } catch (UnsupportedLookAndFeelException ex) {
        log.error("Failed to set LAF", ex);

        ok = false;
    } catch (ClassNotFoundException ex) {
        log.error("Failed to set LAF", ex);

        ok = false;
    } catch (IllegalAccessException ex) {
        log.error("Failed to set LAF", ex);

        ok = false;
    } catch (InstantiationException ex) {
        log.error("Failed to set LAF", ex);

        ok = false;
    }

    return ok;
}

From source file:Main.java

public Main() {
    comboBox = new JComboBox(new String[] { "Select Pet", "Bird", "Cat", "Dog", "Rabbit", "Pig", "Other" });
    add(comboBox, BorderLayout.PAGE_START);
    JFrame frame = new JFrame("Main");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(comboBox);// w  w  w . ja  va  2  s.  c o m
    frame.pack();
    frame.setVisible(true);

    comboBox.showPopup();
    Object child = comboBox.getAccessibleContext().getAccessibleChild(0);
    BasicComboPopup popup = (BasicComboPopup) child;
    popup.setName("BasicComboPopup");
    JList list = popup.getList();
    Container c = SwingUtilities.getAncestorOfClass(JScrollPane.class, list);
    JScrollPane scrollPane = (JScrollPane) c;

    Window mainFrame = SwingUtilities.windowForComponent(comboBox);

    System.out.println(mainFrame.getName());
    Window popupWindow = SwingUtilities.windowForComponent(popup);
    System.out.println(popupWindow);
    Window popupWindowa = SwingUtilities.windowForComponent(c);
    System.out.println(popupWindowa);

    Window mainFrame1 = SwingUtilities.getWindowAncestor(comboBox);

    System.out.println(mainFrame1);
    Window popupWindow1 = SwingUtilities.getWindowAncestor(popup);
    System.out.println(popupWindow1);

    Component mainFrame2 = SwingUtilities.getRoot(comboBox);

    System.out.println(mainFrame2.getName());
    Component popupWindow2 = SwingUtilities.getRoot(popup);
    System.out.println(popupWindow2);

    if (popupWindow != mainFrame) {
        popupWindow.pack();
    }
}