Example usage for java.awt Window setBounds

List of usage examples for java.awt Window setBounds

Introduction

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

Prototype

public void setBounds(int x, int y, int width, int height) 

Source Link

Document

The width or height values are automatically enlarged if either is less than the minimum size as specified by previous call to setMinimumSize .

Usage

From source file:Splash.java

/** Maximize a window, the hard way. */
public static void maximize(Window w) {
    Dimension us = w.getSize(), them = Toolkit.getDefaultToolkit().getScreenSize();
    w.setBounds(0, 0, them.width, them.height);
}

From source file:Main.java

public static void centralizeWindow(Window window) {
    int windowWidth = window.getWidth();
    int windowHeight = window.getHeight();
    int screenWidth = (int) java.awt.Toolkit.getDefaultToolkit().getScreenSize().getWidth();
    int screenHeight = (int) java.awt.Toolkit.getDefaultToolkit().getScreenSize().getHeight();

    window.setBounds(screenWidth / 2 - windowWidth / 2, screenHeight / 2 - windowHeight / 2, windowWidth,
            windowHeight);//from w  w w .  j  ava  2 s .c o m
}

From source file:Main.java

/**
 * Center the given window within the screen boundaries.
 *
 * @param window the window to be centered.
 *///from  w  w  w.  ja  v  a 2 s . co  m
public static void centerWindow(Window window) {
    Rectangle bounds;
    try {
        bounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
                .getDefaultConfiguration().getBounds();
    } catch (Throwable t) {
        Toolkit tk = window.getToolkit();
        Dimension ss = tk.getScreenSize();
        bounds = new Rectangle(ss);
    }

    int width = window.getWidth(), height = window.getHeight();
    window.setBounds(bounds.x + (bounds.width - width) / 2, bounds.y + (bounds.height - height) / 2, width,
            height);
}

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   w  w w  .j  a  v  a2  s.co  m*/
 *
 * @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: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 w  w  w  . j ava2s .  co m
 * @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 = getMaximumWindowBounds();
  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);

}

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   w  w  w .ja 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 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);

}

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.
 * //  w w w  .  j a v  a  2s .  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 = getMaximumWindowBounds();
    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);

}

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

/**
 * @param isInitial/*w  w w .  jav a  2s . c  om*/
 */
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);

}

From source file:edu.ku.brc.ui.UIHelper.java

/**
 * @param window//  w ww  .j  a va2 s.  co  m
 * @param x
 * @param y
 * @param w
 * @param h
 */
public static void positionAndFitToScreen(final java.awt.Window window, final int x, final int y, final int w,
        final int h) {
    Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(window.getGraphicsConfiguration());
    Rectangle winRect = window.getGraphicsConfiguration().getBounds();

    Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
    int xCoord = Math.max(0, x);
    int yCoord = Math.max(0, y);

    int width = w;
    int height = h;

    if ((xCoord + width) > size.width) {
        xCoord = size.width - width;
        xCoord = (winRect.width - width) / 2;
        width = Math.min(winRect.width - screenInsets.left - screenInsets.right, width);
    }

    if (yCoord + height > size.height) {
        yCoord = size.height - x;
        yCoord = (winRect.height - height) / 2;

        height = Math.min(winRect.height - screenInsets.top - screenInsets.bottom, height);
    }
    window.setBounds(xCoord, yCoord, width, height);
}

From source file:processing.app.Base.java

/**
 * Show the About box.//from ww  w .  j  ava 2s. c o m
 */
@SuppressWarnings("serial")
public void handleAbout() {
    final Image image = Theme.getLibImage("about", activeEditor, Theme.scale(475), Theme.scale(300));
    final Window window = new Window(activeEditor) {
        public void paint(Graphics graphics) {
            Graphics2D g = Theme.setupGraphics2D(graphics);
            g.drawImage(image, 0, 0, null);

            Font f = new Font("SansSerif", Font.PLAIN, Theme.scale(11));
            g.setFont(f);
            g.setColor(new Color(0, 151, 156));
            g.drawString(BaseNoGui.VERSION_NAME_LONG, Theme.scale(33), Theme.scale(20));
        }
    };
    window.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            window.dispose();
        }
    });
    int w = image.getWidth(activeEditor);
    int h = image.getHeight(activeEditor);
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    window.setBounds((screen.width - w) / 2, (screen.height - h) / 2, w, h);
    window.setLocationRelativeTo(activeEditor);
    window.setVisible(true);
}