Example usage for java.awt Container getWidth

List of usage examples for java.awt Container getWidth

Introduction

In this page you can find the example usage for java.awt Container getWidth.

Prototype

public int getWidth() 

Source Link

Document

Returns the current width of this component.

Usage

From source file:Main.java

public static void setBoundsAndCenterHorizontally(JComponent component, int x, int y, int width, int height) {
    Container parent = component.getParent();
    int parentWidth = parent.getWidth();
    int paddingOnBothSides = parentWidth - width;
    x = paddingOnBothSides / 2;//from  w ww.java2  s  .co  m
    component.setBounds(x, y, width, height);
}

From source file:Main.java

public static void setScreenCenter(Container container) {
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Dimension screenDimension = toolkit.getScreenSize();
    int width = container.getWidth();
    int height = container.getHeight();
    Point point = new Point();
    point.setLocation((screenDimension.getWidth() - width) / 2, (screenDimension.getHeight() - height) / 2);
    container.setLocation(point);/*  www . j  a v a 2  s.  c  o m*/
}

From source file:Main.java

public static void showOnSameScreenAsMouseCenter(Container frame) {
    Point mouseLocation = MouseInfo.getPointerInfo().getLocation();

    GraphicsDevice device;//from ww  w .j a v  a2  s.c o m

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

    ArrayList<GraphicsDevice> lstDevices = new ArrayList<GraphicsDevice>(lstGDs.length);

    for (GraphicsDevice gd : lstGDs) {
        GraphicsConfiguration gc = gd.getDefaultConfiguration();
        Rectangle screenBounds = gc.getBounds();

        if (screenBounds.contains(mouseLocation)) {
            lstDevices.add(gd);
        }
    }

    if (lstDevices.size() > 0) {
        device = lstDevices.get(0);
    } else {
        device = ge.getDefaultScreenDevice();
    }

    Rectangle bounds = device.getDefaultConfiguration().getBounds();
    frame.setLocation(bounds.x + bounds.width / 2 - frame.getWidth() / 2,
            bounds.y + bounds.height / 2 - frame.getHeight() / 2);
}

From source file:Main.java

public void layoutContainer(Container parent) {
    Insets insets = parent.getInsets();
    int maxWidth = parent.getWidth() - (insets.left + insets.right);
    int maxHeight = parent.getHeight() - (insets.top + insets.bottom);
    int nComps = parent.getComponentCount();
    int previousWidth = 0, previousHeight = 0;
    int x = 0, y = insets.top;
    int rowh = 0, start = 0;
    int xFudge = 0, yFudge = 0;
    boolean oneColumn = false;

    // Go through the components' sizes, if neither
    // preferredLayoutSize nor minimumLayoutSize has
    // been called.
    if (sizeUnknown) {
        setSizes(parent);/*  w  ww.  j  av  a  2s.  co m*/
    }

    if (maxWidth <= minWidth) {
        oneColumn = true;
    }

    if (maxWidth != preferredWidth) {
        xFudge = (maxWidth - preferredWidth) / (nComps - 1);
    }

    if (maxHeight > preferredHeight) {
        yFudge = (maxHeight - preferredHeight) / (nComps - 1);
    }

    for (int i = 0; i < nComps; i++) {
        Component c = parent.getComponent(i);
        if (c.isVisible()) {
            Dimension d = c.getPreferredSize();

            // increase x and y, if appropriate
            if (i > 0) {
                if (!oneColumn) {
                    x += previousWidth / 2 + xFudge;
                }
                y += previousHeight + vgap + yFudge;
            }

            // If x is too large,
            if ((!oneColumn) && (x + d.width) > (parent.getWidth() - insets.right)) {
                // reduce x to a reasonable number.
                x = parent.getWidth() - insets.bottom - d.width;
            }

            // If y is too large,
            if ((y + d.height) > (parent.getHeight() - insets.bottom)) {
                // do nothing.
                // Another choice would be to do what we do to x.
            }

            // Set the component's size and position.
            c.setBounds(x, y, d.width, d.height);

            previousWidth = d.width;
            previousHeight = d.height;
        }
    }
}

From source file:WrapperLayout.java

public void layoutContainer(Container arg0) {
    int count = arg0.getComponentCount();
    if (count > 0) {
        Component child = arg0.getComponent(0);
        java.awt.Insets insets = arg0.getInsets();
        child.setBounds(insets.left, insets.top, arg0.getWidth() - insets.left - insets.right,
                arg0.getHeight() - insets.top - insets.bottom);
    }//from w  w  w .java2  s.c  o m
}

From source file:CenterLayout.java

public void layoutContainer(Container container) {
    int count = container.getComponentCount();
    if (count > 0) {
        Component child = container.getComponent(0);
        java.awt.Insets insets = container.getInsets();
        int availWidth = container.getWidth() - insets.left - insets.right;
        int availHeight = container.getHeight() - insets.top - insets.bottom;
        Dimension preferredSize = child.getPreferredSize();
        double preferredWidth = preferredSize.getWidth();
        double preferredHeight = preferredSize.getHeight();
        int width;
        int height;
        int x;/*  w  ww .  j  av  a 2  s  . c o  m*/
        int y;
        if (preferredWidth < availWidth) {
            x = (int) Math.round(insets.left + (availWidth - preferredWidth) / 2);
            width = (int) Math.round(preferredWidth);
        } else {
            x = insets.left;
            width = availWidth;
        }
        if (preferredHeight < availHeight) {
            y = (int) Math.round(insets.top + (availHeight - preferredHeight) / 2);
            height = (int) Math.round(preferredHeight);
        } else {
            y = insets.top;
            height = availHeight;
        }
        child.setBounds(x, y, width, height);
    }
}

From source file:StackLayout.java

public void layoutContainer(Container parent) {
    Component visibleComp = getVisibleComponent();
    if (visibleComp != null) {
        synchronized (parent.getTreeLock()) {
            Insets insets = parent.getInsets();
            visibleComp.setBounds(insets.left, insets.top, parent.getWidth() - (insets.left + insets.right),
                    parent.getHeight() - (insets.top + insets.bottom));
        }/*from www  . jav a  2  s  . c om*/
    }
}

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;/*from   ww 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:StackLayout.java

private void layoutComponents(Container parent) {
    Component[] components = parent.getComponents();
    Insets in = parent.getInsets();

    int maxHeight = parent.getHeight() - in.top - in.bottom;
    int maxWidth = parent.getWidth() - in.left - in.right;
    boolean horiz = (ourOrientation == HORIZONTAL);

    int totalDepth = calculatePreferredDepth(components, ourOrientation);
    totalDepth = Math.max(totalDepth, (horiz ? maxHeight : maxWidth));

    int prefLength = (ourLengthsMatched ? calculateAdjustedLength(components, ourOrientation, ourSpacing)
            : calculatePreferredLength(components, ourOrientation, ourSpacing));
    int totalLength = Math.min(prefLength, (horiz ? maxWidth : maxHeight));

    int a = (horiz ? in.left : in.top);
    int b = (horiz ? in.top : in.left);
    int l = 0, d = 0, sum = 0;
    int matchedLength = 0;
    Dimension prefsize = null;//from   www. j a v  a  2  s  .  c om

    if (ourLengthsMatched) {
        matchedLength = (horiz ? getMaxPrefWidth(components) : getMaxPrefHeight(components));

        if (prefLength > totalLength && ourSqueezeFactor < 100) {
            int minLength = calculateMinimumLength(components, ourOrientation, ourSpacing);

            if (minLength >= totalLength) {
                matchedLength = (matchedLength * ourSqueezeFactor) / 100;
            }

            else {
                int numSeparators = countSeparators(components);
                int numComponents = components.length - numSeparators;
                int diff = (prefLength - totalLength) / numComponents;
                if ((prefLength - totalLength) % numComponents > 0)
                    diff++;
                matchedLength -= diff;
            }
        }
    }

    for (int i = 0; i < components.length; i++) {
        prefsize = components[i].getPreferredSize();
        if (!ourLengthsMatched)
            l = (horiz ? prefsize.width : prefsize.height);
        else
            l = matchedLength;

        if (components[i] instanceof JSeparator) {
            // l = Math.min(prefsize.width, prefsize.height);
            l = (horiz ? prefsize.width : prefsize.height);
            d = totalDepth;
            sum += l;
            if (ourDrop && sum > totalLength)
                l = 0;
        }

        else {
            sum += l;
            if (ourDrop && sum > totalLength)
                l = 0;

            else if (ourFill && !ourLengthsMatched && i == components.length - 1) {
                l = Math.max(l, (horiz ? maxWidth : maxHeight));
            }

            if (ourDepthsMatched)
                d = totalDepth;
            else
                d = (horiz ? prefsize.height : prefsize.width);
        }

        if (horiz)
            components[i].setBounds(a, b + (totalDepth - d) / 2, l, d);
        else
            components[i].setBounds(b + (totalDepth - d) / 2, a, d, l);

        a += l + ourSpacing;
        sum += ourSpacing;
    }

}

From source file:BeanContainer.java

public void layoutContainer(Container parent) {
        int divider = getDivider(parent);

        Insets insets = parent.getInsets();
        int w = parent.getWidth() - insets.left - insets.right - divider;
        int x = insets.left;
        int y = insets.top;

        for (int k = 1; k < parent.getComponentCount(); k += 2) {
            Component comp1 = parent.getComponent(k - 1);
            Component comp2 = parent.getComponent(k);
            Dimension d = comp2.getPreferredSize();

            comp1.setBounds(x, y, divider - m_hGap, d.height);
            comp2.setBounds(x + divider, y, w, d.height);
            y += d.height + m_vGap;// www  .  j a va2  s .c o m
        }
    }