Example usage for java.awt Container getComponent

List of usage examples for java.awt Container getComponent

Introduction

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

Prototype

public Component getComponent(int n) 

Source Link

Document

Gets the nth component in this container.

Usage

From source file:LCBLayout.java

/**
 * Lays out the components./*  www.  j a v  a  2s .  co m*/
 *
 * @param parent  the parent.
 */
public void layoutContainer(final Container parent) {

    synchronized (parent.getTreeLock()) {
        final Insets insets = parent.getInsets();
        final int ncomponents = parent.getComponentCount();
        final int nrows = ncomponents / COLUMNS;
        for (int c = 0; c < COLUMNS; c++) {
            for (int r = 0; r < nrows; r++) {
                final Component component = parent.getComponent(r * COLUMNS + c);
                final Dimension d = component.getPreferredSize();
                if (this.colWidth[c] < d.width) {
                    this.colWidth[c] = d.width;
                }
                if (this.rowHeight[r] < d.height) {
                    this.rowHeight[r] = d.height;
                }
            }
        }
        int totalHeight = this.vGap * (nrows - 1);
        for (int r = 0; r < nrows; r++) {
            totalHeight = totalHeight + this.rowHeight[r];
        }
        final int totalWidth = this.colWidth[0] + this.colWidth[1] + this.colWidth[2];

        // adjust the width of the second column to use up all of parent
        final int available = parent.getWidth() - insets.left - insets.right - this.labelGap - this.buttonGap;
        this.colWidth[1] = this.colWidth[1] + (available - totalWidth);

        // *** DO THE LAYOUT ***
        int x = insets.left;
        for (int c = 0; c < COLUMNS; c++) {
            int y = insets.top;
            for (int r = 0; r < nrows; r++) {
                final int i = r * COLUMNS + c;
                if (i < ncomponents) {
                    final Component component = parent.getComponent(i);
                    final Dimension d = component.getPreferredSize();
                    final int h = d.height;
                    final int adjust = (this.rowHeight[r] - h) / 2;
                    parent.getComponent(i).setBounds(x, y + adjust, this.colWidth[c], h);
                }
                y = y + this.rowHeight[r] + this.vGap;
            }
            x = x + this.colWidth[c];
            if (c == 0) {
                x = x + this.labelGap;
            }
            if (c == 1) {
                x = x + this.buttonGap;
            }
        }

    }

}

From source file:CircleLayoutTest.java

public void layoutContainer(Container parent) {
    Insets insets = parent.getInsets();
    int containerWidth = parent.getSize().width - insets.left - insets.right;
    int containerHeight = parent.getSize().height - insets.top - insets.bottom;
    int xradius = (containerWidth - maxComponentWidth) / 2;
    int yradius = (containerHeight - maxComponentHeight) / 2;

    setSizes(parent);//  w  w  w. j a  v a  2 s .c  om
    int centerX = insets.left + containerWidth / 2;
    int centerY = insets.top + containerHeight / 2;

    int comCount = parent.getComponentCount();
    for (int i = 0; i < comCount; i++) {
        Component c = parent.getComponent(i);
        if (c.isVisible()) {
            Dimension size = c.getPreferredSize();
            double angle = 2 * Math.PI * i / comCount;
            int x = centerX + (int) (Math.cos(angle) * xradius);
            int y = centerY + (int) (Math.sin(angle) * yradius);

            c.setBounds(x - size.width / 2, y - size.height / 2, size.width, size.height);
        }
    }
}

From source file:FunLayout.java

public void layoutContainer(Container con) {
    int i, count, deltax, deltay, move;
    Dimension conSize;// w  ww  .j  av  a  2  s.  c  om
    Rectangle rect;
    Component comp;

    conSize = con.getSize();
    if (_prevContainerSize == null) {
        _prevContainerSize = conSize;
        return;
    }
    deltax = conSize.width - _prevContainerSize.width;
    deltay = conSize.height - _prevContainerSize.height;
    _prevContainerSize = conSize;
    count = con.countComponents();
    for (i = 0; i < count; i++) {
        comp = con.getComponent(i);
        if (!comp.isVisible())
            continue;
        move = _getMove(comp);
        if (move == 0)
            continue;
        rect = comp.getBounds();
        if (_negSized.containsKey(comp)) {
            // the component is really at a negative size
            rect = (Rectangle) _negSized.get(comp);
            _negSized.remove(comp);
        }
        if ((move & MOVES_RIGHT) > 0)
            rect.x += deltax;
        else if ((move & WIDTH_CHANGES) > 0)
            rect.width += deltax;
        if ((move & MOVES_DOWN) > 0)
            rect.y += deltay;
        else if ((move & HEIGHT_CHANGES) > 0)
            rect.height += deltay;
        // if a components size becomes negative, we track it since the AWT
        // does not allow components to have a size < (0, 0)
        if (rect.width < 0 || rect.height < 0)
            _negSized.put(comp, rect);
        comp.setBounds(rect.x, rect.y, rect.width, rect.height);
    }
}

From source file:RadialLayout.java

/**
 * This is called when the panel is first displayed, and every time its size
 * changes./*from  w ww .j  av a 2  s . c o  m*/
 * Note: You CAN'T assume preferredLayoutSize or minimumLayoutSize will be
 * called -- in the case of applets, at least, they probably won't be.
 *
 * @param  parent  the parent.
 * @see LayoutManager
 */
public void layoutContainer(final Container parent) {
    final Insets insets = parent.getInsets();
    final int maxWidth = parent.getSize().width - (insets.left + insets.right);
    final int maxHeight = parent.getSize().height - (insets.top + insets.bottom);
    final int nComps = parent.getComponentCount();
    int x = 0;
    int y = 0;

    // Go through the components' sizes, if neither preferredLayoutSize nor
    // minimumLayoutSize has been called.
    if (this.sizeUnknown) {
        setSizes(parent);
    }

    if (nComps < 2) {
        final Component c = parent.getComponent(0);
        if (c.isVisible()) {
            final Dimension d = c.getPreferredSize();
            c.setBounds(x, y, d.width, d.height);
        }
    } else {
        double radialCurrent = Math.toRadians(90);
        final double radialIncrement = 2 * Math.PI / nComps;
        final int midX = maxWidth / 2;
        final int midY = maxHeight / 2;
        final int a = midX - this.maxCompWidth;
        final int b = midY - this.maxCompHeight;
        for (int i = 0; i < nComps; i++) {
            final Component c = parent.getComponent(i);
            if (c.isVisible()) {
                final Dimension d = c.getPreferredSize();
                x = (int) (midX - (a * Math.cos(radialCurrent)) - (d.getWidth() / 2) + insets.left);
                y = (int) (midY - (b * Math.sin(radialCurrent)) - (d.getHeight() / 2) + insets.top);

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

From source file:com.diversityarrays.kdxplore.field.FieldViewDialog.java

private void updateControls(boolean enable, Container cont) {
    for (int i = cont.getComponentCount(); --i >= 0;) {
        Component c = cont.getComponent(i);
        if (c instanceof JButton) {
            c.setEnabled(enable);/*from  w  w w . ja  v  a 2s  .  c o m*/
        }
    }
}

From source file:edu.ku.brc.af.core.TaskMgr.java

/**
 * @param parent//w w w .j a va 2  s  . c o  m
 * @param menuItemDesc
 * @param menuPath
 * @param currIndex
 */
public static void buildMenuTree(final MenuElement parent, final MenuItemDesc menuItemDesc,
        final String[] menuPath, final int currIndex) {
    if (currIndex == menuPath.length) {
        MenuElement me = menuItemDesc.getMenuItem();
        if (parent instanceof JMenuBar) {
            ((JMenuBar) parent).add((JMenu) me);

        } else if (parent instanceof JMenu) {
            Container menuComp = ((JMenu) parent).getPopupMenu();
            boolean found = false;
            int insertPos = menuComp.getComponentCount(); // last position
            JMenu menu = (JMenu) parent;

            if (menuItemDesc.getPosition() == MenuItemDesc.Position.Top) {
                insertPos = 0;
                //log.debug(String.format("0 Inserted: %s - %d", ((JMenuItem)me).getText(), insertPos));

            } else if (menuItemDesc.getPosition() == MenuItemDesc.Position.Bottom) {
                //log.debug(String.format("1 Inserted: %s - %d", ((JMenuItem)me).getText(),insertPos));
            }

            if (menuItemDesc.getPosition() == MenuItemDesc.Position.Before
                    || menuItemDesc.getPosition() == MenuItemDesc.Position.After) {
                int inx = 0;
                for (int i = 0; i < menuComp.getComponentCount(); i++) {
                    Component c = menuComp.getComponent(i);
                    if (c instanceof JMenuItem
                            && ((JMenuItem) c).getText().equals(menuItemDesc.getPosMenuItemName())) {
                        insertPos = inx + 1;
                        found = true;
                        break;
                    }
                    inx++;
                }
            }

            if (menuItemDesc.getSepPosition() == MenuItemDesc.Position.Before) {
                menu.add(new JPopupMenu.Separator(), insertPos);
                insertPos++;
            }

            if (insertPos == -1) {
                menu.add((JMenuItem) me);
            } else {
                menu.add((JMenuItem) me, insertPos);
                //log.debug(String.format("2 Inserted: %s - %d", ((JMenuItem)me).getText(),insertPos));
                found = true;
            }

            insertPos++;

            if (menuItemDesc.getSepPosition() == MenuItemDesc.Position.After) {
                if (found) {
                    menu.add(new JPopupMenu.Separator(), insertPos);
                    //log.debug(String.format("3 Inserted: Sep - %d", insertPos));

                }
            }
        } else if (parent instanceof JMenuItem) {
            JMenuItem mi = (JMenuItem) parent;
            JPopupMenu menu = (JPopupMenu) mi.getParent();

            int pos = 0;
            for (int i = 0; i < menu.getComponentCount(); i++) {
                if (mi == menu.getComponent(i)) {
                    pos = i;
                    break;
                }
            }
            //log.debug(String.format("4 Inserted: %s - %d", ((JMenuItem)me).getText(), menuItemDesc.getPosition() == MenuItemDesc.Position.After ? pos + 1 : pos));
            menu.insert((JMenuItem) me,
                    menuItemDesc.getPosition() == MenuItemDesc.Position.After ? pos + 1 : pos);
        }

    } else {
        String label = getResourceString(menuPath[currIndex]);

        MenuElement menuElement = getMenuByName(parent, label);

        /*log.debug(menuPath[currIndex]+" -> "+label+ " "+menuElement);
        if (parent instanceof JMenuItem) log.debug(((JMenuItem)parent).getText());
        else if (parent instanceof JMenu) log.debug(((JMenu)parent).getText());
        else if (parent instanceof JMenuBar) log.debug("MenuBar");
        */

        if (menuElement == null) {
            log.error("Couldn't find menu element [" + label + "]");//$NON-NLS-1$ //$NON-NLS-2$
            //UIRegistry.showError("Couldn't find menu element ["+label+"]"); //$NON-NLS-1$ //$NON-NLS-2$
        }
        buildMenuTree(menuElement, menuItemDesc, menuPath, currIndex + 1);
    }
}

From source file:CircleLayoutTest.java

public void layoutContainer(Container parent) {
    setSizes(parent);/*from   www.  j  av a  2 s  .c o  m*/

    // compute center of the circle

    Insets insets = parent.getInsets();
    int containerWidth = parent.getSize().width - insets.left - insets.right;
    int containerHeight = parent.getSize().height - insets.top - insets.bottom;

    int xcenter = insets.left + containerWidth / 2;
    int ycenter = insets.top + containerHeight / 2;

    // compute radius of the circle

    int xradius = (containerWidth - maxComponentWidth) / 2;
    int yradius = (containerHeight - maxComponentHeight) / 2;
    int radius = Math.min(xradius, yradius);

    // lay out components along the circle

    int n = parent.getComponentCount();
    for (int i = 0; i < n; i++) {
        Component c = parent.getComponent(i);
        if (c.isVisible()) {
            double angle = 2 * Math.PI * i / n;

            // center point of component
            int x = xcenter + (int) (Math.cos(angle) * radius);
            int y = ycenter + (int) (Math.sin(angle) * radius);

            // move component so that its center is (x, y)
            // and its size is its preferred size
            Dimension d = c.getPreferredSize();
            c.setBounds(x - d.width / 2, y - d.height / 2, d.width, d.height);
        }
    }
}

From source file:BeanContainer.java

protected int getDivider(Container parent) {
        if (m_divider > 0)
            return m_divider;

        int divider = 0;
        for (int k = 0; k < parent.getComponentCount(); k += 2) {
            Component comp = parent.getComponent(k);
            Dimension d = comp.getPreferredSize();
            divider = Math.max(divider, d.width);
        }//from   www.ja va 2s .c o  m
        divider += m_hGap;
        return divider;
    }

From source file:BeanContainer.java

public Dimension preferredLayoutSize(Container parent) {
        int divider = getDivider(parent);

        int w = 0;
        int h = 0;
        for (int k = 1; k < parent.getComponentCount(); k += 2) {
            Component comp = parent.getComponent(k);
            Dimension d = comp.getPreferredSize();
            w = Math.max(w, d.width);
            h += d.height + m_vGap;//ww w.j a  v a2 s. c  om
        }
        h -= m_vGap;

        Insets insets = parent.getInsets();
        return new Dimension(divider + w + insets.left + insets.right, h + insets.top + insets.bottom);
    }

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;//from   ww  w  . j a  v  a  2s. co m
        }
    }