public Dimension getPreferredSize() { Dimension size = super.getPreferredSize(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Insets scnMax = Toolkit.getDefaultToolkit().getScreenInsets(getOwner().getGraphicsConfiguration()); int taskBarSize = scnMax.bottom; int usableScreenHeight = screenSize.height - taskBarSize - 50; if (size.height > (usableScreenHeight)) { size.height = usableScreenHeight; } return size; }
/** * Finds out the monitor where the user currently has the input focus. * This method is usually used to help the client code to figure out on * which monitor it should place newly created windows/frames/dialogs. * * @return the GraphicsConfiguration of the monitor which currently has the * input focus */ private static GraphicsConfiguration getCurrentGraphicsConfiguration() { Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); if (focusOwner != null) { Window w = SwingUtilities.getWindowAncestor(focusOwner); if (w != null) { return w.getGraphicsConfiguration(); } else { //#217737 - try to find the main window which could be placed in secondary screen for( Frame f : Frame.getFrames() ) { if( "NbMainWindow".equals(f.getName())) { //NOI18N return f.getGraphicsConfiguration(); } } } } return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); }
/** * Finds out the monitor where the user currently has the input focus. * This method is usually used to help the client code to figure out on * which monitor it should place newly created windows/frames/dialogs. * * @return the GraphicsConfiguration of the monitor which currently has the * input focus */ private static GraphicsConfiguration getCurrentGraphicsConfiguration() { Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); if (focusOwner != null) { Window w = SwingUtilities.getWindowAncestor(focusOwner); if (w != null) { return w.getGraphicsConfiguration(); } else { //#217737 - try to find the main window which could be placed in secondary screen for( Frame f : Frame.getFrames() ) { if( "NbMainWindow".equals(f.getName())) { //NOI18N return f.getGraphicsConfiguration(); } } } } return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); }
w.setBounds(w.getGraphicsConfiguration().getBounds()); w.setVisible(true);
Window w=new Window(null) { @Override public void paint(Graphics g) { final Font font = getFont().deriveFont(48f); g.setFont(font); g.setColor(Color.RED); final String message = "Hello"; FontMetrics metrics = g.getFontMetrics(); g.drawString(message, (getWidth()-metrics.stringWidth(message))/2, (getHeight()-metrics.getHeight())/2); } @Override public void update(Graphics g) { paint(g); } }; w.setAlwaysOnTop(true); w.setBounds(w.getGraphicsConfiguration().getBounds()); w.setBackground(new Color(0, true)); w.setVisible(true);
public GraphicsDevice getMonitorWindowIsOn(Window window) { // First, grab the x,y position of the Window object GraphicsConfiguration windowGraphicsConfig = window.getGraphicsConfiguration(); if (windowGraphicsConfig == null) { return null; // Should probably be handled better } Rectangle windowBounds = windowGraphicsConfig.getBounds(); for (GraphicsDevice gd : ge.getScreenDevices()) { Rectangle monitorBounds = gd.getDefaultConfiguration().getBounds(); if (monitorBounds.contains(windowBounds.x, windowBounds.y)) { return gd; } } // um, return null I guess, should make this default better though, // maybe to the default screen device, except, I am sure if no monitors are // connected that may be null as well. return null; }
/** * getScreenTotalArea, This returns the total area of the screen. (The total area includes any * task bars.) This function accounts for multi-monitor setups. If a window is supplied, then * the the monitor that contains the window will be used. If a window is not supplied, then the * primary monitor will be used. */ static public Rectangle getScreenTotalArea(Window windowOrNull) { Rectangle bounds; if (windowOrNull == null) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); bounds = ge.getDefaultScreenDevice().getDefaultConfiguration().getBounds(); } else { GraphicsConfiguration gc = windowOrNull.getGraphicsConfiguration(); bounds = gc.getBounds(); } return bounds; }
/** * Computes the screen's insets for the specified window and returns them. * <p> * While this might seem strange, screen insets can change from one window * to another. For example, on X11 windowing systems, there is no guarantee that * a window will be displayed on the same screen, let alone computer, as the one * the application is running on. * </p> * @param window the window for which screen insets should be computed. * @return the screen's insets for the specified window */ public static Insets getScreenInsets(Window window) { return Toolkit.getDefaultToolkit().getScreenInsets(window.getGraphicsConfiguration()); }
/** * Computes the screen's insets for the specified window and returns them. * <p> * While this might seem strange, screen insets can change from one window * to another. For example, on X11 windowing systems, there is no guarantee that * a window will be displayed on the same screen, let alone computer, as the one * the application is running on. * </p> * @param window the window for which screen insets should be computed. * @return the screen's insets for the specified window */ public static Insets getScreenInsets(Window window) { return Toolkit.getDefaultToolkit().getScreenInsets(window.getGraphicsConfiguration()); }
/** * Returns the maximum dimensions for a full-screen window. * * @param window window who's full screen size should be computed. * @return the maximum dimensions for a full-screen window */ public static Rectangle getFullScreenBounds(Window window) { Toolkit toolkit; Dimension screenSize; toolkit = Toolkit.getDefaultToolkit(); screenSize = toolkit.getScreenSize(); Insets screenInsets = toolkit.getScreenInsets(window.getGraphicsConfiguration()); return new Rectangle(screenInsets.left, screenInsets.top, screenSize.width-screenInsets.left-screenInsets.right, screenSize.height-screenInsets.top-screenInsets.bottom); } }
/** * Calculates default location for the specified window. * @return default location for the window * @param window the window location is calculated for. * It should not be null. */ public static Point defaultLocation(Window window) { GraphicsConfiguration gc = window.getGraphicsConfiguration(); Rectangle bounds = gc.getBounds(); Insets insets = window.getToolkit().getScreenInsets(gc); int x = bounds.x + insets.left; int y = bounds.y + insets.top; return new Point(x, y); }
static public Insets getScreenInsets(Window wnd) { Insets si; try { if(wnd==null) { si=Toolkit.getDefaultToolkit().getScreenInsets(new Frame().getGraphicsConfiguration()); } else { si=wnd.getToolkit() .getScreenInsets(wnd.getGraphicsConfiguration()); } } catch(NoSuchMethodError thr) { si=new Insets(0,0,0,0); } return si; }
private static Point fitInScreen(Point p, Window w, int lineHeight) { Rectangle screenSize = w.getGraphicsConfiguration().getBounds(); if(p.y + w.getHeight() >= screenSize.height) p.y = p.y - w.getHeight() - lineHeight; return p; } //}}}
private static Rectangle getUsableDeviceBounds(Window window) { Window owner = window.getOwner(); GraphicsConfiguration gc = null; if (owner == null) { gc = GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice().getDefaultConfiguration(); } else { gc = owner.getGraphicsConfiguration(); } Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc); Rectangle bounds = gc.getBounds(); bounds.x += insets.left; bounds.y += insets.top; bounds.width -= (insets.left + insets.right); bounds.height -= (insets.top + insets.bottom); return bounds; }
/** * Finds out the monitor where the user currently has the input focus. * This method is usually used to help the client code to figure out on * which monitor it should place newly created windows/frames/dialogs. * * @return the GraphicsConfiguration of the monitor which currently has the * input focus */ private static GraphicsConfiguration getCurrentGraphicsConfiguration() { Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); if (focusOwner != null) { Window w = SwingUtilities.getWindowAncestor(focusOwner); if (w != null) { return w.getGraphicsConfiguration(); } } return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); }
private static Point fitInScreen(Point p, Window w, int lineHeight) { Rectangle screenSize = w.getGraphicsConfiguration().getBounds(); if(p.y + w.getHeight() >= screenSize.height) p.y = p.y - w.getHeight() - lineHeight; return p; } //}}}
public static void ensureWindowIsVisible( Window w ) { int width = w.getWidth(); int height = w.getHeight(); Toolkit toolkit = Toolkit.getDefaultToolkit(); GraphicsConfiguration gc = w.getGraphicsConfiguration(); Insets screenInsets = toolkit.getScreenInsets( gc ); Rectangle screenSize = gc.getBounds(); w.setLocation( Math.max( screenInsets.left, Math.min( w.getX(), screenSize.width - screenInsets.right - width ) ), Math.max( screenInsets.top, Math.min( w.getY(), screenSize.height - screenInsets.bottom - height ) ) ); }
public static void restoreFullScreenWindow(Window window) { Rectangle bounds = fullScreenBounds.remove(window); if (bounds != null) { window.setBounds(bounds); } else { GraphicsDevice graphicsDevice = window.getGraphicsConfiguration().getDevice(); if (graphicsDevice.isFullScreenSupported()) graphicsDevice.setFullScreenWindow(null); } }
public static void centerOnScreen(Window target) { DisplayMode displayMode = target.getGraphicsConfiguration().getDevice().getDisplayMode(); int displayHeight = displayMode.getHeight(); int displayWidth = displayMode.getWidth(); Dimension targetSize = target.getSize(); Point frameLocation = new Point(displayWidth / 2 - targetSize.width / 2, displayHeight / 2 - targetSize.height / 2); target.setLocation(frameLocation); }
public static void centerToScreen(final Window window) { if (window == null) { return; } final DisplayMode displayMode = window.getGraphicsConfiguration().getDevice().getDisplayMode(); final Point location = getCenterToScreenLocation(displayMode, window.getSize()); window.setLocation(location); return; }