Example usage for java.awt Window getPreferredSize

List of usage examples for java.awt Window getPreferredSize

Introduction

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

Prototype

public Dimension getPreferredSize() 

Source Link

Document

Returns the preferred size of this container.

Usage

From source file:Main.java

/** Gets the dimension of this window were it to be repacked. */
public static Dimension getRepackSize(final Window w) {
    final Dimension size = w.getSize();
    final Dimension pref = w.getPreferredSize();
    if (size.width >= pref.width && size.height >= pref.height)
        return size;
    return new Dimension(size.width < pref.width ? pref.width : size.width,
            size.height < pref.height ? pref.height : size.height);
}

From source file:Main.java

/**
 * Centers a window on screen.//from   www .  j  a v  a 2 s .c o  m
 * <p>
 * @param w     The window to center.
 */
public static void centerOnScreen(Window w) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension splashSize = w.getPreferredSize();
    w.setLocation(screenSize.width / 2 - (splashSize.width / 2),
            screenSize.height / 2 - (splashSize.height / 2));
}

From source file:Main.java

public static Window showCentered(Component parent, Window window) {
    if (null == window)
        throw new IllegalArgumentException("window null");

    if (window instanceof Dialog) {
        Dialog dialog = (Dialog) window;

        boolean isResizable = dialog.isResizable();

        dialog.setResizable(true);//from   w  ww.  ja  v a  2 s  .  c o  m
        dialog.pack();
        dialog.setResizable(isResizable);
    } else {
        window.pack();
    }

    Dimension windowSize = window.getPreferredSize();

    int newX;
    int newY;

    if (null == parent) {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

        newX = (screen.width - windowSize.width) / 2;
        newY = (screen.height - windowSize.height) / 2;
    } else {
        Dimension parentSize = parent.getSize();
        Point loc = parent.getLocation();

        newX = (parentSize.width - windowSize.width) / 2 + loc.x;
        newY = (parentSize.height - windowSize.height) / 2 + loc.y;

        if (0 > newX)
            newX = 0;
        if (0 > newY)
            newY = 0;
    }

    window.setLocation(newX, newY);
    window.setVisible(true);

    return window;
}

From source file:Main.java

public static Window showBelow(Component parent, Window window, int horizontalAlignment) {
    final int DISTANCE = 2;

    if (null == parent)
        throw new IllegalArgumentException("parent null");
    if (null == window)
        throw new IllegalArgumentException("parent null");
    if (!((SwingConstants.LEADING == horizontalAlignment) || (SwingConstants.TRAILING == horizontalAlignment)
            || (SwingConstants.CENTER == horizontalAlignment)
            || (SwingConstants.SOUTH == horizontalAlignment))) {

        throw new IllegalArgumentException("Illegal horizontal alignment " + horizontalAlignment
                + " should be either SwingConstants.LEADING or "
                + "SwingConstants.TRAILING or SwingConstants.CENTER");
    }/*  w  ww  .j a  va  2  s  .c  om*/

    window.pack();

    Dimension windowSize = window.getPreferredSize();
    Dimension parentSize = parent.getSize();

    Point loc = parent.getLocationOnScreen();

    int newX;

    if ((SwingConstants.CENTER == horizontalAlignment) || (SwingConstants.SOUTH == horizontalAlignment)) {

        newX = (parentSize.width - windowSize.width) / 2 + loc.x;
    } else if (SwingConstants.TRAILING == horizontalAlignment) {
        newX = loc.x + parentSize.width - windowSize.width;
    } else {
        newX = loc.x;
    }

    window.setLocation(newX, (loc.y + parentSize.height + DISTANCE));

    window.setVisible(true);

    return window;
}

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

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

}