Example usage for java.awt Component setSize

List of usage examples for java.awt Component setSize

Introduction

In this page you can find the example usage for java.awt Component setSize.

Prototype

public void setSize(int width, int height) 

Source Link

Document

Resizes this component so that it has width width and height height .

Usage

From source file:Main.java

/**
 * Use this static method if you want to center and set its position
 * compared to the size of the current users screen size. Valid percent is
 * between +-(0-100) minus is treated as plus, bigger than 100 is always set
 * to 100.//from   w  ww.ja v  a  2s . c o  m
 *
 * @param component
 *            the component you want to center and set size on
 * @param percentOfScreen
 *            the percent of the current screensize you want the component
 *            to be
 */
public static void centerComponentInWindow(Component component, int percentOfScreen) {
    if (percentOfScreen < 0) {
        centerComponentInWindow(component, -percentOfScreen);
        return;
    }
    if (percentOfScreen > 100) {
        centerComponentInWindow(component, 100);
        return;
    }
    double percent = percentOfScreen / 100.d;
    Dimension dimension = component.getToolkit().getScreenSize();
    component.setSize((int) (dimension.getWidth() * percent), (int) (dimension.getHeight() * percent));
    centerComponentInWindow(component);
}

From source file:VerticalFlowLayout.java

/**
 *  Description of the Method//from   www. j a  v  a 2  s.  c om
 *
 *@param  target  Description of Parameter
 */
public void layoutContainer(Container target) {
    synchronized (target.getTreeLock()) {
        Insets insets = target.getInsets();
        int maxheight = target.getHeight() - (insets.top + insets.bottom + _vgap * 2);
        int nmembers = target.getComponentCount();
        int y = 0;

        Dimension preferredSize = preferredLayoutSize(target);
        Dimension targetSize = target.getSize();

        switch (_valign) {
        case TOP:
            y = insets.top;
            break;
        case CENTER:
            y = (targetSize.height - preferredSize.height) / 2;
            break;
        case BOTTOM:
            y = targetSize.height - preferredSize.height - insets.bottom;
            break;
        }

        for (int i = 0; i < nmembers; i++) {
            Component m = target.getComponent(i);
            if (m.isVisible()) {
                Dimension d = m.getPreferredSize();
                m.setSize(d.width, d.height);

                if ((y + d.height) <= maxheight) {
                    if (y > 0) {
                        y += _vgap;
                    }

                    int x = 0;
                    switch (_halign) {
                    case LEFT:
                        x = insets.left;
                        break;
                    case CENTER:
                        x = (targetSize.width - d.width) / 2;
                        break;
                    case RIGHT:
                        x = targetSize.width - d.width - insets.right;
                        break;
                    }

                    m.setLocation(x, y);

                    y += d.getHeight();

                } else {
                    break;
                }
            }
        }
    }
}

From source file:edu.uara.gui.tableeditor.ChartGenerationFrame.java

private void panelAncestorResized(java.awt.event.HierarchyEvent evt) {
    //handle resize
    //        Component parent = evt.getChangedParent();
    Component parent = this.splitPane;
    Component comp = evt.getComponent();

    if (comp != null && parent != null) {
        comp.setSize(parent.getWidth(), this.splitPane.getDividerLocation());
        String s = String.format("Current Width = %1$s" + " Height = %2$s", comp.getWidth(), comp.getHeight());

        this.updateStatus(s);

    }/*from  www  . ja v a 2s . c o  m*/
}

From source file:EdgeLayoutExample.java

public void layoutContainer(Container parent) {
    synchronized (parent.getTreeLock()) {
        Insets insets = parent.getInsets();
        int top = insets.top;
        int left = insets.left;

        Dimension minimumSize = minimumLayoutSize(parent);

        int height = minimumSize.height;
        int width = minimumSize.width;

        int availableHeight = parent.getHeight() - insets.bottom - insets.top;
        int availableWidth = parent.getWidth() - insets.left - insets.right;
        if (height < availableHeight) {
            height = availableHeight;/*  w  w  w  .ja v a 2 s .  c om*/
        }
        if (width < availableWidth) {
            width = availableWidth;
        }

        int bottom = availableHeight;
        int right = availableWidth;

        Dimension preferredSize = preferredLayoutSize(parent);

        int preferredWidthAvailable = width - preferredSize.width;
        int preferredHeightAvailable = height - preferredSize.height;

        Component centerComp = null;

        for (int i = 0; i < this.components.size(); i++) {
            Component c = (Component) this.components.get(i);
            String constraint = (String) this.constraints.get(c);

            if (constraint.equals(CENTER)) {
                centerComp = c;
            } else {
                int compHeight;
                int compWidth;
                int xOrigin;
                int yOrigin;

                if (constraint.equals(NORTH) || constraint.equals(SOUTH)) {
                    compWidth = width;

                    if (preferredHeightAvailable > 0) {
                        int preferredHeightNeeded = c.getPreferredSize().height - c.getMinimumSize().height;
                        if (preferredHeightAvailable > preferredHeightNeeded) {
                            compHeight = c.getPreferredSize().height;
                            preferredHeightAvailable -= preferredHeightNeeded;
                        } else {
                            compHeight = c.getMinimumSize().height + preferredHeightAvailable;
                            preferredHeightAvailable = 0;
                        }
                    } else {
                        compHeight = c.getMinimumSize().height;
                    }
                    height = height - compHeight;

                    xOrigin = left;

                    if (constraint.equals(NORTH)) {
                        yOrigin = top;
                        top += compHeight;
                    } else {
                        yOrigin = bottom - compHeight;
                        bottom = yOrigin;
                    }
                } else {
                    compHeight = height;
                    if (preferredWidthAvailable > 0) {
                        int preferredWidthNeeded = c.getPreferredSize().width - c.getMinimumSize().width;
                        if (preferredWidthAvailable > preferredWidthNeeded) {
                            compWidth = c.getPreferredSize().width;
                            preferredWidthAvailable -= preferredWidthNeeded;
                        } else {
                            compWidth = c.getMinimumSize().width + preferredWidthAvailable;
                            preferredWidthAvailable = 0;
                        }
                    } else {
                        compWidth = c.getMinimumSize().width;
                    }
                    width = width - compWidth;

                    yOrigin = top;

                    if (constraint.equals(WEST)) {
                        xOrigin = left;
                        left += compWidth;
                    } else {
                        xOrigin = right - compWidth;
                        right = xOrigin;
                    }
                }
                c.setSize(compWidth, compHeight);
                c.setBounds(xOrigin, yOrigin, compWidth, compHeight);
            }
            if (centerComp != null) {
                c.setSize(width, height);
                c.setBounds(left, top, width, height);
            }
        }
    }
}

From source file:org.objectstyle.cayenne.modeler.pref.ComponentGeometry.java

void updateSize(Component c, int initialWidth, int initialHeight) {
    int w = getIntWidth(initialWidth);
    int h = getIntHeight(initialHeight);

    if (w > 0 && h > 0) {
        c.setSize(w, h);
    }/* w w  w . ja v  a 2s  . com*/
}

From source file:org.openmicroscopy.shoola.util.ui.UIUtilities.java

/**
 * Sets the location of the specified child relative to the location
 * of the specified parent and then makes it visible, and size to fill window.
 * This method is mainly useful for windows, frames and dialogs. 
 * //from   w w w  .ja v a2 s .  c om
 * @param parentBounds    The bounds of the visible parent.
 * @param child     The child to display.
 * @param max      The maximum size of the window.
 */
public static void setLocationRelativeToAndSizeToWindow(Rectangle parentBounds, Component child,
        Dimension max) {
    if (child == null)
        return;
    if (parentBounds == null)
        parentBounds = new Rectangle(0, 0, 5, 5);
    if (max == null)
        max = new Dimension(5, 5);
    int x = (int) (parentBounds.getX() + parentBounds.getWidth());
    int y = (int) parentBounds.getY();
    int childWidth = child.getWidth();
    int childHeight = child.getHeight();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    if (x + childWidth > screenSize.getWidth()) {
        if (childWidth < parentBounds.getX())
            x = (int) (parentBounds.getX()) - childWidth;
        else
            x = (int) (screenSize.getWidth() - childWidth);
    }
    child.setLocation(x, y);
    int newHeight = (int) screenSize.getHeight() - y - 10;
    int newWidth = (int) screenSize.getWidth() - x - 10;

    if (newWidth > childWidth)
        childWidth = newWidth;
    if (newHeight > childHeight)
        childHeight = newHeight;

    if (childWidth > max.getWidth())
        childWidth = (int) max.getWidth();
    if (childHeight > max.getHeight())
        childHeight = (int) max.getHeight();

    child.setSize(childWidth, childHeight);
    child.setVisible(true);
}

From source file:pcgen.gui2.tools.Utility.java

/**
 * Centers a {@code JFrame} to the screen.
 *
 * @param frame   JFrame frame to center
 * @param isPopup boolean is the frame a popup dialog?
 *///from   w  w w  . j a v a  2s.  co m
public static void centerComponent(Component frame, boolean isPopup) {
    // since the Toolkit.getScreenSize() method is broken in the Linux implementation
    // of Java 5  (it returns double the screen size under xinerama), this method is
    // encapsulated to accomodate this with a hack.
    // TODO: remove the hack, once Java fixed this.
    // final Dimension screenSize = getScreenSize(Toolkit.getDefaultToolkit());
    final Rectangle screenSize = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
            .getDefaultConfiguration().getBounds();

    if (isPopup) {
        frame.setSize(screenSize.width / 2, screenSize.height / 2);
    }

    final Dimension frameSize = frame.getSize();

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

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

    frame.setLocation(screenSize.x + ((screenSize.width - frameSize.width) / 2),
            screenSize.y + ((screenSize.height - frameSize.height) / 2));
}