Example usage for java.awt Window getBounds

List of usage examples for java.awt Window getBounds

Introduction

In this page you can find the example usage for java.awt Window getBounds.

Prototype

public Rectangle getBounds() 

Source Link

Document

Gets the bounds of this component in the form of a Rectangle object.

Usage

From source file:Main.java

public static void centerFrame(final Window frame) {
    final Rectangle bounds = frame.getBounds();
    final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    bounds.x = (screen.width / 2) - (bounds.width / 2);
    bounds.y = (screen.height / 2) - (bounds.height / 2);
    frame.setBounds(bounds);//  ww  w.j  a va2 s .  c  o m
}

From source file:Main.java

/**
 * {@link Window#pack() Packs} the given window and positions it so that its
 * center stays the same.//  w  ww . java2  s  .  c o  m
 *
 * @param window
 *            The window to pack and recenter
 */
public static void repackCentered(Window window) {
    Point center = getCenter(window.getBounds());
    window.pack();
    window.setLocation((center.x - window.getWidth() / 2), (center.y - window.getHeight() / 2));
    window.repaint();
}

From source file:Main.java

/** Centers the given window within the specified parent window. */
public static void centerWindow(final Window parent, final Window window) {
    centerWindow(parent.getBounds(), window);
}

From source file:Main.java

public static void setWindowLocationRelativeTo(Window window, Window relativeWindow) {
    Rectangle windowBounds = window.getBounds();
    Dimension rwSize = relativeWindow.getSize();
    Point rwLoc = relativeWindow.getLocation();

    int dx = rwLoc.x + ((rwSize.width - windowBounds.width) >> 1);
    int dy = rwLoc.y + ((rwSize.height - windowBounds.height) >> 1);
    Dimension ss = window.getToolkit().getScreenSize();

    if (dy + windowBounds.height > ss.height) {
        dy = ss.height - windowBounds.height;
        dx = rwLoc.x < (ss.width >> 1) ? rwLoc.x + rwSize.width : rwLoc.x - windowBounds.width;
    }/*from w  w w  .j  a  v a  2s . com*/
    if (dx + windowBounds.width > ss.width) {
        dx = ss.width - windowBounds.width;
    }
    if (dx < 0) {
        dx = 0;
    }
    if (dy < 0) {
        dy = 0;
    }
    window.setLocation(dx, dy);
}

From source file:Main.java

/**
 *  Updates the first window's position to center it with respect to the
 *  second window. If the first window is larger than the second, it will
 *  be offset to the top/left as needed, but not exceeding the bounds of
 *  the screen./*w  w  w  .jav  a  2  s  . c  o m*/
 *  <p>
 *  The second window may be null, in which case the first is centered within
 *  the screen. This is a convenience for ownerless dialogs.
 */
public static void center(Window window, Window inWindow) {
    if (inWindow == null) {
        center(window);
    } else {
        center(window, inWindow.getBounds(), true);
    }
}

From source file:Main.java

private static Rectangle getScreenBounds(Window aWindow) {

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] devices = ge.getScreenDevices();

    if (aWindow == null)
        return devices[0].getDefaultConfiguration().getBounds();

    Rectangle bounds = aWindow.getBounds();
    int centerX = (int) bounds.getCenterX();
    int centerY = (int) bounds.getCenterY();

    for (GraphicsDevice device : devices) {
        GraphicsConfiguration gc = device.getDefaultConfiguration();
        Rectangle rect = gc.getBounds();
        if (rect.contains(centerX, centerY))
            return rect;
    }//from www. java 2  s . co  m

    return null;
}

From source file:AWTUtilities.java

/**
 * Reposition the specified window so that it necessarily fits on the screen,
 * while trying to minimize changes.//  ww w .  j  ava2  s  . c  o  m
 */

public static void forceToScreen(Window window) {
    Dimension screenSize = window.getToolkit().getScreenSize();
    Rectangle bounds = window.getBounds();

    bounds.width = Math.min(bounds.width, screenSize.width);
    bounds.height = Math.min(bounds.height, screenSize.height);
    bounds.x = Math.min(Math.max(bounds.x, 0), screenSize.width - bounds.width);
    bounds.y = Math.min(Math.max(bounds.y, 0), screenSize.height - bounds.height);

    window.setBounds(bounds);
}

From source file:Main.java

/**
 * Positions the specified frame at a relative position in the screen, where 50% is considered to be the center of the
 * screen./*from ww w. j  a  v a2s .  c om*/
 *
 * @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 spaceOnX = Math.max(s.width - f.width, 0);
    final int spaceOnY = Math.max(s.height - f.height, 0);
    final int x = (int) (horizontalPercent * spaceOnX) + s.x;
    final int y = (int) (verticalPercent * spaceOnY) + s.y;
    frame.setBounds(x, y, f.width, f.height);
    frame.setBounds(s.intersection(frame.getBounds()));
}

From source file:net.pms.newgui.components.WindowProperties.java

private boolean updateWindowBounds(@Nullable Window eventWindow) {
    if (eventWindow != window) {
        return false;
    }/*ww w .  j  a  va2  s .c  om*/
    int state = eventWindow instanceof Frame ? ((Frame) eventWindow).getExtendedState() : 0;
    Rectangle bounds;
    if (state == 0) {
        bounds = eventWindow.getBounds();
    } else if ((state & Frame.MAXIMIZED_BOTH) != Frame.MAXIMIZED_BOTH) {
        bounds = eventWindow.getBounds();
        // Don't store maximized dimensions
        if ((state & Frame.MAXIMIZED_HORIZ) != 0) {
            bounds.x = windowBounds.x;
            bounds.width = windowBounds.width;
        } else if ((state & Frame.MAXIMIZED_VERT) != 0) {
            bounds.y = windowBounds.y;
            bounds.height = windowBounds.height;
        }
    } else {
        bounds = windowBounds;
    }
    boolean changed = !bounds.equals(windowBounds);
    if (changed) {
        windowBounds = bounds;
    }
    if (windowState != (byte) state) {
        windowState = (byte) state;
        changed = true;
    }
    return changed;
}

From source file:edu.ku.brc.specify.config.init.secwiz.UserPanel.java

/**
 * @param isInitial//from   w w w . j  a v a 2s.  c o  m
 */
private void loadData(final boolean isInitial) {
    label.setText("");

    String hostName = properties.getProperty("hostName");
    String dbUserName = properties.getProperty("dbUserName");
    String dbPassword = properties.getProperty("dbPassword");

    int index = 0;
    List<String> dbNamesList = masterPanel.getDbNamesForMaster();
    List<String> otherNamesList = masterPanel.getDbNameList();

    if (dbNamesList == null || dbNamesList.size() == 0) {
        return;
    }

    //dbNamesList.clear();
    //otherNamesList.clear();
    //dbNamesList.add("testfish");

    Vector<String> items = new Vector<String>(dbNamesList);
    Collections.sort(items);

    if (!isInitial) {
        index = dbList.getSelectedIndex();
        if (index == -1) {
            return;
        }
    }

    databaseName = isInitial ? items.get(0) : (String) dbList.getSelectedValue();

    DBMSUserMgr mgr = DBMSUserMgr.getInstance();
    do {
        if (mgr.connect(dbUserName, dbPassword, hostName, databaseName)) {
            if (isInitial) {
                HashSet<String> dbNameHashSet = new HashSet<String>();
                DefaultListModel model = new DefaultListModel();
                for (String nm : items) {
                    model.addElement(nm);
                    dbNameHashSet.add(nm);
                }
                dbList.setModel(model);

                model = new DefaultListModel();
                for (String nm : otherNamesList) {
                    if (!dbNameHashSet.contains(nm)) {
                        model.addElement(nm);
                    }
                }
                otherDBList.setModel(model);
            }

            label.setText(getFormattedResStr("MSTR_USR_DB", databaseName));

            if (!mgr.doesDBHaveTable(databaseName, "specifyuser")) {
                items.remove(0);
                databaseName = isInitial ? items.get(0) : (String) dbList.getSelectedValue();
                continue;
            }

            Vector<UserData> userDataList = new Vector<UserData>();
            String sql = "SELECT SpecifyUserId, Name, Password, EMail FROM specifyuser";
            Vector<Object[]> data = BasicSQLUtils.query(mgr.getConnection(), sql);

            for (Object[] c : data) {
                UserData ud = new UserData((Integer) c[0], (String) c[1], (String) c[2], "(On user's machine)",
                        (String) c[3]);
                userDataList.add(ud);

                sql = String.format(
                        "SELECT LastName, FirstName, EMail FROM agent WHERE SpecifyUserID = %d ORDER BY TimestampModified, TimestampCreated LIMIT 0,1",
                        ud.getId());
                Vector<Object[]> uData = BasicSQLUtils.query(mgr.getConnection(), sql);
                if (uData.size() > 0) {
                    Object[] d = uData.get(0);
                    ud.setLastName((String) d[0]);
                    ud.setFirstName((String) d[1]);

                    String email = (String) d[2];
                    if (StringUtils.isNotEmpty(email) && StringUtils.isEmpty(ud.getEmail())) {
                        ud.setEmail(email);
                    }
                } else {
                    // error
                }
            }
            mgr.close();
            userModel.setUserData(userDataList);
            UIHelper.calcColumnWidths(userTable);

            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    Window window = getTopWindow();
                    Insets screenInsets = Toolkit.getDefaultToolkit()
                            .getScreenInsets(window.getGraphicsConfiguration());
                    Rectangle screenRect = window.getGraphicsConfiguration().getBounds();

                    screenRect.height -= screenInsets.top + screenInsets.bottom;
                    screenRect.width -= screenInsets.left + screenInsets.right;

                    Rectangle rect = window.getBounds();
                    Dimension size = window.getPreferredSize();

                    // Make sure the window isn't larger than the screen
                    size.width = Math.min(size.width, screenRect.width);
                    size.height = Math.min(size.height, screenRect.height);

                    if (size.height > rect.height || size.width > rect.width) {
                        window.setBounds(rect.x, rect.y, size.width, size.height);
                        UIHelper.centerWindow(getTopWindow());

                    }
                }
            });

            if (isInitial && items.size() > 0) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        dbList.setSelectedIndex(0);
                    }
                });
            }

            break;
        } else if (items.size() > 1) {
            items.remove(0);
            databaseName = isInitial ? items.get(0) : (String) dbList.getSelectedValue();
        } else {
            break;
        }

    } while (true);

}