Java Code Examples for java.awt.Window#getSize()

The following examples show how to use java.awt.Window#getSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WindowSizeEnforcer.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/** @param window The window to enforce min/max size on. */
public static void enforce(Window window) {
    Dimension origSize  = window.getSize();
    Dimension otherSize = window.getMinimumSize();
    int       width     = origSize.width;
    int       height    = origSize.height;

    if (width < otherSize.width) {
        width = otherSize.width;
    }
    if (height < otherSize.height) {
        height = otherSize.height;
    }
    otherSize = window.getMaximumSize();
    if (width > otherSize.width) {
        width = otherSize.width;
    }
    if (height > otherSize.height) {
        height = otherSize.height;
    }
    if (width != origSize.width || height != origSize.height) {
        window.setSize(width, height);
    }
}
 
Example 2
Source File: DemoUtil.java    From littleluck with Apache License 2.0 6 votes vote down vote up
public static void centerWindowOnScreen(Window window)
{
    final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    final Dimension size = window.getSize();

    if (size.height > screenSize.height)
    {
        size.height = screenSize.height;
    }

    if (size.width > screenSize.width)
    {
        size.width = screenSize.width;
    }

    window.setLocation((screenSize.width - size.width) / 2,
            (screenSize.height - size.height) / 2);
}
 
Example 3
Source File: GraphicUtils.java    From xyTalk-pc with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
    * Sets the location of the specified window so that it is centered on
    * screen.
    * 
    * @param window
    *            The window to be centered.
    */
   public static void centerWindowOnScreen(Window window) {
final Dimension screenSize = Toolkit.getDefaultToolkit()
	.getScreenSize();
final Dimension size = window.getSize();

if (size.height > screenSize.height) {
    size.height = screenSize.height;
}

if (size.width > screenSize.width) {
    size.width = screenSize.width;
}

window.setLocation((screenSize.width - size.width) / 2,
	(screenSize.height - size.height) / 2);
   }
 
Example 4
Source File: RefineryUtilities.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Positions the specified frame at a relative position in the screen, 
 * where 50% is considered to be the center of the screen.
 *
 * @param frame  the frame.
 * @param horizontalPercent  the relative horizontal position of the frame 
 *     (0.0 to 1.0, where 0.5 is the center of the screen).
 * @param verticalPercent  the relative vertical position of the frame 
 *     (0.0 to 1.0, where 0.5 is the center of the screen).
 */
public static void positionFrameOnScreen(Window frame,
                                         double horizontalPercent,
                                         double verticalPercent) {

    Rectangle s = getMaximumWindowBounds();
    Dimension f = frame.getSize();
    int w = Math.max(s.width - f.width, 0);
    int h = Math.max(s.height - f.height, 0);
    int x = (int) (horizontalPercent * w) + s.x;
    int y = (int) (verticalPercent * h) + s.y;
    frame.setBounds(x, y, f.width, f.height);

}
 
Example 5
Source File: WinCenter.java    From Java with Artistic License 2.0 5 votes vote down vote up
public static void center(Window win){
	Toolkit tkit = Toolkit.getDefaultToolkit();
	Dimension sSize = tkit.getScreenSize();
	Dimension wSize = win.getSize();
	if(wSize.height > sSize.height){
		wSize.height = sSize.height;
	}
	if(wSize.width > sSize.width){
		wSize.width = sSize.width;
	}
	win.setLocation((sSize.width - wSize.width)/ 2, (sSize.height - wSize.height)/ 2);
}
 
Example 6
Source File: UIUtilities.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
public static void centerOnScreen(Window window) {
  Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  Dimension size = window.getSize();
  window.setLocation(
    (screenSize.width - size.width) / 2,
    (screenSize.height - size.height) / 2);
}
 
Example 7
Source File: PropertiesPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void makeDialogWider(int delta) {
    Window w = SwingUtilities.getWindowAncestor(this);
    if (w != null) {
        Dimension size = w.getSize();
        size.width += delta;
        w.setSize(size);
    }
}
 
Example 8
Source File: RemoteAWTService.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static Snapshot[] getGUISnapshots() {
    List snapshots = new ArrayList();   //System.err.println("gGUI: thread = "+Thread.currentThread());
    Window[] windows = Window.getWindows(); //System.err.println("gGUI: windows = "+windows.length);
    for (int wi = 0; wi < windows.length; wi++) {
        Window w = windows[wi]; //System.err.println("gGUI: w["+wi+"] = "+w+", is visible = "+w.isVisible());
        if (!w.isVisible()) {
            continue;
        }
        Dimension d = w.getSize();  //System.err.println("gGUI:  size = "+d);
        if (d.width == 0 || d.height == 0) {
            continue;
        }
        BufferedImage bi = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = bi.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_DEFAULT);
        w.paint(g);
        Raster raster = bi.getData();
        Object data = raster.getDataElements(0, 0, d.width, d.height, null);
        int[] dataArr;  //System.err.println("gGUI: data = "+data);
        if (data instanceof int[]) {
            dataArr = (int[]) data;
        } else {
            continue;
        }
        String title = null;
        if (w instanceof Frame) {
            title = ((Frame) w).getTitle();
        } else if (w instanceof Dialog) {
            title = ((Dialog) w).getTitle();
        }   //System.err.println("gGUI: title = "+title);
        snapshots.add(new Snapshot(w, title, d.width, d.height, dataArr));
    }
    Snapshot[] snapshotArr = (Snapshot[]) snapshots.toArray(new Snapshot[] {});
    lastGUISnapshots = snapshotArr;
    return snapshotArr;
}
 
Example 9
Source File: Session.java    From FancyBing with GNU General Public License v3.0 5 votes vote down vote up
private void saveWidthHeight(Window window, String name)
{
    Preferences prefs = createNode(name);
    if (prefs == null)
        return;
    Dimension size = window.getSize();
    prefs.putInt("width", size.width);
    prefs.putInt("height", size.height);
}
 
Example 10
Source File: RefineryUtilities.java    From ccu-historian with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Positions the specified frame at a relative position in the screen, where 50% is considered
 * to be the center of the screen.
 *
 * @param frame  the frame.
 * @param horizontalPercent  the relative horizontal position of the frame (0.0 to 1.0,
 *                           where 0.5 is the center of the screen).
 * @param verticalPercent  the relative vertical position of the frame (0.0 to 1.0, where
 *                         0.5 is the center of the screen).
 */
public static void positionFrameOnScreen(final Window frame,
                                         final double horizontalPercent,
                                         final double verticalPercent) {

    final Rectangle s = frame.getGraphicsConfiguration().getBounds();
    final Dimension f = frame.getSize();
    final int w = Math.max(s.width - f.width, 0);
    final int h = Math.max(s.height - f.height, 0);
    final int x = (int) (horizontalPercent * w) + s.x;
    final int y = (int) (verticalPercent * h) + s.y;
    frame.setBounds(x, y, f.width, f.height);

}
 
Example 11
Source File: SwingHelper.java    From universal-tween-engine with Apache License 2.0 5 votes vote down vote up
/**
 * Centers a component according to the window location.
 * @param wnd The parent window
 * @param cmp A component, usually a dialog
 */
public static void centerInWindow(Window wnd, Component cmp) {
	Dimension size = wnd.getSize();
	Point loc = wnd.getLocationOnScreen();
	Dimension cmpSize = cmp.getSize();
	loc.x += (size.width  - cmpSize.width)/2;
	loc.y += (size.height - cmpSize.height)/2;
	cmp.setBounds(loc.x, loc.y, cmpSize.width, cmpSize.height);
}
 
Example 12
Source File: FullScreenInsets.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public static void main(final String[] args) {
    final GraphicsEnvironment ge = GraphicsEnvironment
            .getLocalGraphicsEnvironment();
    final GraphicsDevice[] devices = ge.getScreenDevices();

    final Window wGreen = new Frame();
    wGreen.setBackground(Color.GREEN);
    wGreen.setSize(300, 300);
    wGreen.setVisible(true);
    sleep();
    final Insets iGreen = wGreen.getInsets();
    final Dimension sGreen = wGreen.getSize();

    final Window wRed = new Frame();
    wRed.setBackground(Color.RED);
    wRed.setSize(300, 300);
    wRed.setVisible(true);
    sleep();
    final Insets iRed = wGreen.getInsets();
    final Dimension sRed = wGreen.getSize();

    for (final GraphicsDevice device : devices) {
        if (!device.isFullScreenSupported()) {
            continue;
        }
        device.setFullScreenWindow(wGreen);
        sleep();
        testWindowBounds(device.getDisplayMode(), wGreen);
        testColor(wGreen, Color.GREEN);

        device.setFullScreenWindow(wRed);
        sleep();
        testWindowBounds(device.getDisplayMode(), wRed);
        testColor(wRed, Color.RED);

        device.setFullScreenWindow(null);
        sleep();
        testInsets(wGreen.getInsets(), iGreen);
        testInsets(wRed.getInsets(), iRed);
        testSize(wGreen.getSize(), sGreen);
        testSize(wRed.getSize(), sRed);
    }
    wGreen.dispose();
    wRed.dispose();
    if (!passed) {
        throw new RuntimeException("Test failed");
    }
}
 
Example 13
Source File: Util.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static Point getTitlePoint(Window decoratedWindow) {
    Point p = decoratedWindow.getLocationOnScreen();
    Dimension d = decoratedWindow.getSize();
    return new Point(p.x + (int)(d.getWidth()/2),
                     p.y + (int)(decoratedWindow.getInsets().top/2));
}
 
Example 14
Source File: ShowUsagesAction.java    From otto-intellij-plugin with Apache License 2.0 4 votes vote down vote up
private void setSizeAndDimensions(@NotNull JTable table,
    @NotNull JBPopup popup,
    @NotNull RelativePoint popupPosition,
    @NotNull List<UsageNode> data) {
  JComponent content = popup.getContent();
  Window window = SwingUtilities.windowForComponent(content);
  Dimension d = window.getSize();

  int width = calcMaxWidth(table);
  width = (int)Math.max(d.getWidth(), width);
  Dimension headerSize = ((AbstractPopup)popup).getHeaderPreferredSize();
  width = Math.max((int)headerSize.getWidth(), width);
  width = Math.max(myWidth, width);

  if (myWidth == -1) myWidth = width;
  int newWidth = Math.max(width, d.width + width - myWidth);

  myWidth = newWidth;

  int rowsToShow = Math.min(30, data.size());
  Dimension dimension = new Dimension(newWidth, table.getRowHeight() * rowsToShow);
  Rectangle rectangle = fitToScreen(dimension, popupPosition, table);
  dimension = rectangle.getSize();
  Point location = window.getLocation();
  if (!location.equals(rectangle.getLocation())) {
    window.setLocation(rectangle.getLocation());
  }

  if (!data.isEmpty()) {
    TableScrollingUtil.ensureSelectionExists(table);
  }
  table.setSize(dimension);
  //table.setPreferredSize(dimension);
  //table.setMaximumSize(dimension);
  //table.setPreferredScrollableViewportSize(dimension);


  Dimension footerSize = ((AbstractPopup)popup).getFooterPreferredSize();

  int newHeight = (int)(dimension.height + headerSize.getHeight() + footerSize.getHeight()) + 4/* invisible borders, margins etc*/;
  Dimension newDim = new Dimension(dimension.width, newHeight);
  window.setSize(newDim);
  window.setMinimumSize(newDim);
  window.setMaximumSize(newDim);

  window.validate();
  window.repaint();
  table.revalidate();
  table.repaint();
}
 
Example 15
Source File: FullScreenInsets.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(final String[] args) {
    final GraphicsEnvironment ge = GraphicsEnvironment
            .getLocalGraphicsEnvironment();
    final GraphicsDevice[] devices = ge.getScreenDevices();

    final Window wGreen = new Frame();
    wGreen.setBackground(Color.GREEN);
    wGreen.setSize(300, 300);
    wGreen.setVisible(true);
    sleep();
    final Insets iGreen = wGreen.getInsets();
    final Dimension sGreen = wGreen.getSize();

    final Window wRed = new Frame();
    wRed.setBackground(Color.RED);
    wRed.setSize(300, 300);
    wRed.setVisible(true);
    sleep();
    final Insets iRed = wGreen.getInsets();
    final Dimension sRed = wGreen.getSize();

    for (final GraphicsDevice device : devices) {
        if (!device.isFullScreenSupported()) {
            continue;
        }
        device.setFullScreenWindow(wGreen);
        sleep();
        testWindowBounds(device.getDisplayMode(), wGreen);
        testColor(wGreen, Color.GREEN);

        device.setFullScreenWindow(wRed);
        sleep();
        testWindowBounds(device.getDisplayMode(), wRed);
        testColor(wRed, Color.RED);

        device.setFullScreenWindow(null);
        sleep();
        testInsets(wGreen.getInsets(), iGreen);
        testInsets(wRed.getInsets(), iRed);
        testSize(wGreen.getSize(), sGreen);
        testSize(wRed.getSize(), sRed);
    }
    wGreen.dispose();
    wRed.dispose();
    if (!passed) {
        throw new RuntimeException("Test failed");
    }
}
 
Example 16
Source File: Util.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static Point getTitlePoint(Window decoratedWindow) {
    Point p = decoratedWindow.getLocationOnScreen();
    Dimension d = decoratedWindow.getSize();
    return new Point(p.x + (int)(d.getWidth()/2),
                     p.y + (int)(decoratedWindow.getInsets().top/2));
}
 
Example 17
Source File: FullScreenInsets.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public static void main(final String[] args) {
    final GraphicsEnvironment ge = GraphicsEnvironment
            .getLocalGraphicsEnvironment();
    final GraphicsDevice[] devices = ge.getScreenDevices();

    final Window wGreen = new Frame();
    wGreen.setBackground(Color.GREEN);
    wGreen.setSize(300, 300);
    wGreen.setVisible(true);
    sleep();
    final Insets iGreen = wGreen.getInsets();
    final Dimension sGreen = wGreen.getSize();

    final Window wRed = new Frame();
    wRed.setBackground(Color.RED);
    wRed.setSize(300, 300);
    wRed.setVisible(true);
    sleep();
    final Insets iRed = wGreen.getInsets();
    final Dimension sRed = wGreen.getSize();

    for (final GraphicsDevice device : devices) {
        if (!device.isFullScreenSupported()) {
            continue;
        }
        device.setFullScreenWindow(wGreen);
        sleep();
        testWindowBounds(device.getDisplayMode(), wGreen);
        testColor(wGreen, Color.GREEN);

        device.setFullScreenWindow(wRed);
        sleep();
        testWindowBounds(device.getDisplayMode(), wRed);
        testColor(wRed, Color.RED);

        device.setFullScreenWindow(null);
        sleep();
        testInsets(wGreen.getInsets(), iGreen);
        testInsets(wRed.getInsets(), iRed);
        testSize(wGreen.getSize(), sGreen);
        testSize(wRed.getSize(), sRed);
    }
    wGreen.dispose();
    wRed.dispose();
    if (!passed) {
        throw new RuntimeException("Test failed");
    }
}
 
Example 18
Source File: Util.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static Point getTitlePoint(Window decoratedWindow) {
    Point p = decoratedWindow.getLocationOnScreen();
    Dimension d = decoratedWindow.getSize();
    return new Point(p.x + (int)(d.getWidth()/2),
                     p.y + (int)(decoratedWindow.getInsets().top/2));
}
 
Example 19
Source File: Util.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static Point getTitlePoint(Window decoratedWindow) {
    Point p = decoratedWindow.getLocationOnScreen();
    Dimension d = decoratedWindow.getSize();
    return new Point(p.x + (int)(d.getWidth()/2),
                     p.y + (int)(decoratedWindow.getInsets().top/2));
}
 
Example 20
Source File: Util.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static Point getTitlePoint(Window decoratedWindow) {
    Point p = decoratedWindow.getLocationOnScreen();
    Dimension d = decoratedWindow.getSize();
    return new Point(p.x + (int)(d.getWidth()/2),
                     p.y + (int)(decoratedWindow.getInsets().top/2));
}