Java examples for Swing:Window
ensure Window Inside Graphics Device
import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.Insets; import java.awt.Point; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.Window; import java.awt.geom.Rectangle2D; import com.phpsysinfo.psidesktop.business.utils.TaskbarInformation.TaskbarLocation; public class Main{ public static void ensureWindowInsideGraphicsDevice(Point clicPoint, Point windowLocation, Window window) { GraphicsDevice gd = getClickedDevice(clicPoint); if (gd == null) return; Rectangle gdBounds = gd.getDefaultConfiguration().getBounds(); if (windowLocation.getX() + window.getWidth() > gdBounds.getWidth()) { // Window go outside - right of the screen windowLocation.x -= window.getWidth() + windowLocation.getX() - gdBounds.getWidth() + 10; windowLocation.x += gdBounds.getX(); } else if (windowLocation.getX() - gdBounds.getX() < 0) { // Window go outside - left of the screen windowLocation.x = (int) gdBounds.getX() + 2; }/*from www .j a va2 s .co m*/ // Window go outside - top of the screen if (windowLocation.getY() - gdBounds.getY() < 0) { windowLocation.y = (int) (clicPoint.getY() + gdBounds.getY() + 20); } } /** * Return on which screen the pointer was when the clic was done * @param clicPoint * @return */ public static GraphicsDevice getClickedDevice(final Point clicPoint) { Rectangle2D size = new Rectangle2D.Double(); GraphicsEnvironment localGE = GraphicsEnvironment .getLocalGraphicsEnvironment(); for (GraphicsDevice gd : localGE.getScreenDevices()) { for (GraphicsConfiguration graphicsConfiguration : gd .getConfigurations()) { Rectangle2D.union(size, graphicsConfiguration.getBounds(), size); if (size.contains(clicPoint)) return gd; } } return null; } }