Example usage for java.awt Dialog setBounds

List of usage examples for java.awt Dialog setBounds

Introduction

In this page you can find the example usage for java.awt Dialog setBounds.

Prototype

public void setBounds(int x, int y, int width, int height) 

Source Link

Document

The width or height values are automatically enlarged if either is less than the minimum size as specified by previous call to setMinimumSize .

Usage

From source file:Main.java

/**
 * Positions the specified dialog at a position relative to its parent.
 *
 * @param dialog  the dialog to be positioned.
 * @param horizontalPercent  the relative location.
 * @param verticalPercent  the relative location.
 *///from w  ww.  j a v  a2  s .  c  o m
public static void positionDialogRelativeToParent(final Dialog dialog, final double horizontalPercent,
        final double verticalPercent) {
    final Dimension d = dialog.getSize();
    final Container parent = dialog.getParent();
    final Dimension p = parent.getSize();

    final int baseX = parent.getX() - d.width;
    final int baseY = parent.getY() - d.height;
    final int w = d.width + p.width;
    final int h = d.height + p.height;
    int x = baseX + (int) (horizontalPercent * w);
    int y = baseY + (int) (verticalPercent * h);

    // make sure the dialog fits completely on the screen...
    final Rectangle s = getMaximumWindowBounds();
    x = Math.min(x, (s.width - d.width));
    x = Math.max(x, 0);
    y = Math.min(y, (s.height - d.height));
    y = Math.max(y, 0);

    dialog.setBounds(x + s.x, y + s.y, d.width, d.height);

}