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(Rectangle r) 

Source Link

Document

The r.width or r.height values will be 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.
 */// w  w  w .j a v a 2s  .  c o m
public static void positionDialogRelativeToParent(final Dialog dialog, final double horizontalPercent,
        final double verticalPercent) {
    final Container parent = dialog.getParent();
    if (parent == null || (parent.isVisible() == false)) {
        positionFrameOnScreen(dialog, horizontalPercent, verticalPercent);
        return;
    }

    final Dimension d = dialog.getSize();
    final Dimension p = parent.getSize();

    final int baseX = parent.getX();
    final int baseY = parent.getY();

    final int parentPointX = baseX + (int) (horizontalPercent * p.width);
    final int parentPointY = baseY + (int) (verticalPercent * p.height);

    final int dialogPointX = Math.max(0, parentPointX - (int) (horizontalPercent * d.width));
    final int dialogPointY = Math.max(0, parentPointY - (int) (verticalPercent * d.height));

    // make sure the dialog fits completely on the screen...
    final Rectangle s = parent.getGraphicsConfiguration().getBounds();
    final Rectangle r = new Rectangle(dialogPointX, dialogPointY, d.width, d.height);
    final Rectangle intersectedDialogBounds = r.intersection(s);
    if (intersectedDialogBounds.width < d.width) {
        r.x = s.width - d.width;
        r.width = d.width;
    }
    if (intersectedDialogBounds.height < d.height) {
        r.y = s.height - d.height;
        r.height = d.height;
    }
    final Rectangle finalIntersection = r.intersection(s);
    dialog.setBounds(finalIntersection);
}

From source file:org.pentaho.reporting.libraries.designtime.swing.LibSwingUtil.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.
 *///  www  .ja v a  2 s  . com
public static void positionDialogRelativeToParent(final Dialog dialog, final double horizontalPercent,
        final double verticalPercent) {
    final Container parent = dialog.getParent();
    if (parent == null || (parent.isVisible() == false)) {
        positionFrameOnScreen(dialog, horizontalPercent, verticalPercent);
        return;
    }

    final Dimension d = dialog.getSize();
    final Dimension p = parent.getSize();

    final int baseX = parent.getX();
    final int baseY = parent.getY();

    final int parentPointX = baseX + (int) (horizontalPercent * p.width);
    final int parentPointY = baseY + (int) (verticalPercent * p.height);

    final int dialogPointX = parentPointX - (int) (horizontalPercent * d.width);
    final int dialogPointY = parentPointY - (int) (verticalPercent * d.height);

    // make sure the dialog fits completely on the screen...
    final Rectangle s = parent.getGraphicsConfiguration().getBounds();
    final Rectangle r = new Rectangle(dialogPointX, dialogPointY, d.width, d.height);
    final Rectangle intersectedDialogBounds = r.intersection(s);
    if (intersectedDialogBounds.width < d.width) {
        r.x = s.width - d.width;
        r.width = d.width;
    }
    if (intersectedDialogBounds.height < d.height) {
        r.y = s.height - d.height;
        r.height = d.height;
    }
    final Rectangle finalIntersection = r.intersection(s);
    dialog.setBounds(finalIntersection);
}