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:Main.java

public static void swapComponentsAndResizeUI(JComponent ui, JComponent current, JComponent next) {
    ui.remove(current);//  w  w w .j  av a  2s. c  om
    ui.add(next);
    current = next;
    Component c = ui.getTopLevelAncestor();
    if (c instanceof Window) {
        Window w = (Window) c;
        w.pack();
    }
}

From source file:Main.java

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

From source file:Main.java

/**
 * This method packs, sets the size, and sets the position of the window given
 *
 * @param window/*from ww w . j  a v a  2s . c om*/
 */
public static void centerAndPack(Window window) {
    window.pack();
    int x = getWindowXCenter() - (window.getWidth() / 2);
    int y = getWindowYCenter() - (window.getHeight() / 2);
    window.setLocation(x, y);
    window.setMinimumSize(new Dimension(window.getWidth(), window.getHeight()));
}

From source file:Main.java

/** Shortcut for the boilerplate code to pack and display a window (such as a {@code JFrame}). */
public static void displayWindow(Window w) {
    w.pack();
    w.setVisible(true);// ww  w  .j  av a2s . c o m
}

From source file:Main.java

public static void pack(Component c) {
    Window window = getFrame(c);
    window.pack();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension bounds = new Dimension();
    bounds.width = Math.min(window.getWidth(), screenSize.width * 8 / 10);
    bounds.height = Math.min(window.getHeight(), screenSize.height * 8 / 10);
    window.setSize(bounds);/* ww  w .j  ava2 s .  c o m*/
}

From source file:Main.java

public static void packLater(final Window win, final Component parent) {
    win.pack();
    win.setLocationRelativeTo(parent);//w  w w  .  j a  va  2  s  . c  o  m
    win.addWindowListener(new WindowAdapter() {
        @Override
        public void windowOpened(WindowEvent e) {
            win.pack();
            win.setLocationRelativeTo(parent);
        }
    });
}

From source file:Main.java

static public void centerOnParent(final Window child, final boolean absolute) {
    child.pack();
    boolean useChildsOwner = child.getOwner() != null
            ? ((child.getOwner() instanceof JFrame) || (child.getOwner() instanceof JDialog))
            : false;//from   w  w  w . j a  va 2  s  . c o m
    final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    final Dimension parentSize = useChildsOwner ? child.getOwner().getSize() : screenSize;
    final Point parentLocationOnScreen = useChildsOwner ? child.getOwner().getLocationOnScreen()
            : new Point(0, 0);
    final Dimension childSize = child.getSize();
    childSize.width = Math.min(childSize.width, screenSize.width);
    childSize.height = Math.min(childSize.height, screenSize.height);
    child.setSize(childSize);
    int x;
    int y;
    if ((child.getOwner() != null) && child.getOwner().isShowing()) {
        x = (parentSize.width - childSize.width) / 2;
        y = (parentSize.height - childSize.height) / 2;
        x += parentLocationOnScreen.x;
        y += parentLocationOnScreen.y;
    } else {
        x = (screenSize.width - childSize.width) / 2;
        y = (screenSize.height - childSize.height) / 2;
    }
    if (!absolute) {
        x /= 2;
        y /= 2;
    }
    child.setLocation(x, y);
}

From source file:Main.java

/**
 * {@link Window#pack() Packs} the given window and positions it so that its
 * center stays the same./*  w ww.  j  a v a  2s  .c o m*/
 *
 * @param window
 *            The window to pack and recenter
 */
public static void repackCentered(Window window) {
    Point center = getCenter(window.getBounds());
    window.pack();
    window.setLocation((center.x - window.getWidth() / 2), (center.y - window.getHeight() / 2));
    window.repaint();
}

From source file:Main.java

public static void showWindow(final Window window, boolean modal) {
    Runnable r = new Runnable() {
        public void run() {
            window.pack();
            window.setVisible(true);//from   w w w .  j  a va 2 s .  co m
        }
    };

    if (modal) {
        try {
            SwingUtilities.invokeAndWait(r);
            window.dispose();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } else {
        SwingUtilities.invokeLater(r);
    }
}

From source file:Main.java

/**
 * Center window on screen.// w ww  . j  a  v a2 s  .c  o  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(final Window window, final 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();
    }

    window.setLocationRelativeTo(null);
}