Example usage for java.awt Window getSize

List of usage examples for java.awt Window getSize

Introduction

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

Prototype

public Dimension getSize() 

Source Link

Document

Returns the size of this component in the form of a Dimension object.

Usage

From source file:Main.java

/**
 * Positions the specified frame at a relative position in the screen, where
 * 50% is considered to be the center of the screen.
 * //  w w w. jav a  2 s .co  m
 * @param frame
 *          the frame.
 * @param horizontalPercent
 *          the relative horizontal position of the frame (0.0 to 1.0, where
 *          0.5 is the center of the screen).
 * @param verticalPercent
 *          the relative vertical position of the frame (0.0 to 1.0, where 0.5
 *          is the center of the screen).
 */
public static void positionFrameOnScreen(final Window frame, final double horizontalPercent,
        final double verticalPercent) {

    final Rectangle s = getMaximumWindowBounds();
    final Dimension f = frame.getSize();
    final int w = Math.max(s.width - f.width, 0);
    final int h = Math.max(s.height - f.height, 0);
    final int x = (int) (horizontalPercent * w) + s.x;
    final int y = (int) (verticalPercent * h) + s.y;
    frame.setBounds(x, y, f.width, f.height);

}

From source file:Main.java

/**
 * Calculates the position of a frame to be in the center of an other frame.
 *
 * @param parentFrame//from   ww w.  j a v a  2 s .  c  o  m
 * @param frame
 * @return
 */
public static Point getCenter(final Component parentFrame, final Window frame) {
    final Point point = new Point();
    int x = 0, y = 0;

    if (parentFrame == null || frame == null) {
        point.setLocation(x, y);
        return point;
    }

    x = parentFrame.getLocation().x + parentFrame.getSize().width / 2 - frame.getSize().width / 2;
    y = parentFrame.getLocation().y + parentFrame.getSize().height / 2 - frame.getSize().height / 2;

    point.setLocation(x, y);

    return point;
}

From source file:com.digitalgeneralists.assurance.ui.factories.DialogFactory.java

private static JDialog createDialogInstance(Window parent, ModalityType modality,
        IDialogResponseHandler responseHandler, IDialogPanel contentPane, Dimension size) {
    if (size == null) {
        size = new Dimension((parent.getSize().width / 2), (parent.getSize().height / 2));
    }/* w  ww .  ja va 2  s .  co m*/
    AssuranceDialog dialog = new AssuranceDialog(parent, contentPane.getDialogTitle(),
            Dialog.ModalityType.DOCUMENT_MODAL, responseHandler);
    dialog.setResizable(false);
    dialog.setSize(size);
    dialog.setLocationRelativeTo(parent);
    if (contentPane instanceof Container) {
        dialog.setContentPane((Container) contentPane);
    }

    return dialog;
}

From source file:Main.java

/**
 * Zentriert ein Window relativ zu dem Parent-Window
 * /* w w  w  . ja v a  2s.  c om*/
 * @param parent
 *            Parent-Window, wenn null, dann wird relativ zu dem Bildschirm
 *            zentriert
 * @param child
 *            Window das zentrirt werden soll.
 */
static public void centreWindow(Window parent, Window child) {
    if (child == null)
        return;
    Point parentLocation = null;
    Dimension parentSize = null;
    if (parent == null) {
        parentLocation = new Point(0, 0);
        parentSize = child.getToolkit().getScreenSize();
    } else {
        parentLocation = parent.getLocationOnScreen();
        parentSize = parent.getSize();
    }
    Dimension childSize = child.getSize();
    child.setLocation((int) (parentLocation.getX() + parentSize.getWidth() / 2 - childSize.getWidth() / 2),
            (int) (parentLocation.getY() + parentSize.getHeight() / 2 - childSize.getHeight() / 2));
}

From source file:GUIUtils.java

/**
 * Centers <CODE>wind</CODE> within the screen. If centering would cause the
 * title bar to go off the top of the screen then move the window down.
 * /*from  ww  w .j a va2 s  .c  o m*/
 * @param wind
 *          The Window to be centered.
 * 
 * @throws IllegalArgumentException
 *           If <TT>wind</TT> is <TT>null</TT>.
 */
public static void centerWithinScreen(Window wind) {
    if (wind == null) {
        throw new IllegalArgumentException("null Window passed");
    }
    final Toolkit toolKit = Toolkit.getDefaultToolkit();
    final Rectangle rcScreen = new Rectangle(toolKit.getScreenSize());
    final Dimension windSize = wind.getSize();
    final Dimension parentSize = new Dimension(rcScreen.width, rcScreen.height);
    if (windSize.height > parentSize.height) {
        windSize.height = parentSize.height;
    }
    if (windSize.width > parentSize.width) {
        windSize.width = parentSize.width;
    }
    center(wind, rcScreen);
}

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>.
 *///w  ww .  j a  va  2 s  . co 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: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();//from   w  w w.  j  a  v a  2s  .com
    } 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:Main.java

/**
 * Center window on screen./*from ww w .  jav a  2 s .  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:it.geosolutions.imageio.plugins.nitronitf.ImageIOUtils.java

public static void centerWindow(Window w) {
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension window = w.getSize();
    if (window.width == 0)
        return;//from   w w w  .j a  va 2s .c om
    int left = screen.width / 2 - window.width / 2;
    int top = (screen.height - window.height) / 4;
    if (top < 0)
        top = 0;
    w.setLocation(left, top);
}

From source file:Main.java

static public void centerOnParent(final Window child, final boolean absolute) {
    child.pack();/* w ww  .  jav a 2s .c o  m*/
    boolean useChildsOwner = child.getOwner() != null
            ? ((child.getOwner() instanceof JFrame) || (child.getOwner() instanceof JDialog))
            : false;
    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);
}