Java JDialog poseInsideScreen(JDialog component)

Here you can find the source of poseInsideScreen(JDialog component)

Description

Poses component inside the screen bounds

License

Open Source License

Parameter

Parameter Description
component - JDialog component which is posed

Declaration

public static void poseInsideScreen(JDialog component) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.JDialog;

public class Main {
    /**// w ww  . j a  va  2s  .c om
     * Poses component inside the screen bounds
     * @param component - JDialog component which is posed
     */
    public static void poseInsideScreen(JDialog component) {
        Rectangle componentBounds = component.getBounds();
        Rectangle screenBounds = getVirtualBounds();
        Point newLocation;
        int newLocationX;
        int newLocationY;

        if (!isInsideScreen(component)) {
            if (componentBounds.getX() < 0)
                newLocationX = 0;
            else if (componentBounds.getX() + componentBounds.getWidth() > screenBounds.getWidth())
                newLocationX = screenBounds.width - componentBounds.width;
            else
                newLocationX = componentBounds.x;

            if (componentBounds.getY() < 0)
                newLocationY = 0;
            else if (componentBounds.getY() + componentBounds.getHeight() > screenBounds.getHeight())
                newLocationY = screenBounds.height - componentBounds.height;
            else
                newLocationY = componentBounds.y;

            newLocation = new Point(newLocationX, newLocationY);
            component.setLocation(newLocation);
        }
    }

    /**
     * Returns the screen bounds.
     * @return bounds of the screen
     */
    public static Rectangle getVirtualBounds() {
        Rectangle bounds = new Rectangle(0, 0, 0, 0);
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice lstGDs[] = ge.getScreenDevices();
        for (GraphicsDevice gd : lstGDs) {
            bounds.add(gd.getDefaultConfiguration().getBounds());
        }
        return bounds;
    }

    /**
     * Checks if component is inside the screen bounds
     * @param component - JDialog component which position is checked
     * @return true if component inside the screen bounds, false otherwise
     */
    public static boolean isInsideScreen(JDialog component) {
        boolean inside;
        Rectangle virtualBounds = getVirtualBounds();
        inside = virtualBounds.contains(component.getBounds());
        return inside;
    }
}

Related

  1. isInsideScreen(JDialog component)
  2. jointButton(JDialog frame, final JButton button)
  3. makeJDialogCancellable(final Window w, final Action cancelAction, final boolean disposeOnCancel)
  4. messageDialog(String string, JDialog parentDialog)
  5. openDialog(final JDialog dialog)
  6. positionDialogInContainer(JDialog dialog, Container frame, int horizontal, int vertical)
  7. renderDialog(JDialog thedialog, String szMessage, int noffsetx, int noffsety)
  8. resizeDialogToScreen(JDialog dialog)
  9. rptaConfirmDialog(JDialog pJDialog, String pMensaje)