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:SpringBox.java

/**
 * Aligns the first <code>rows</code> * <code>cols</code> components of
 * <code>parent</code> in a grid. Each component is as big as the maximum
 * preferred width and height of the components. The parent is made just big
 * enough to fit them all./*from   w w w .  ja  v  a 2s.c om*/
 * 
 * @param rows
 *          number of rows
 * @param cols
 *          number of columns
 * @param initialX
 *          x location to start the grid at
 * @param initialY
 *          y location to start the grid at
 * @param xPad
 *          x padding between cells
 * @param yPad
 *          y padding between cells
 */
public static void makeGrid(Container parent, int rows, int cols, int initialX, int initialY, int xPad,
        int yPad) {
    SpringLayout layout;
    try {
        layout = (SpringLayout) parent.getLayout();
    } catch (ClassCastException exc) {
        System.err.println("The first argument to makeGrid must use SpringLayout.");
        return;
    }

    Spring xPadSpring = Spring.constant(xPad);
    Spring yPadSpring = Spring.constant(yPad);
    Spring initialXSpring = Spring.constant(initialX);
    Spring initialYSpring = Spring.constant(initialY);
    int max = rows * cols;

    // Calculate Springs that are the max of the width/height so that all
    // cells have the same size.
    Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)).getWidth();
    Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)).getWidth();
    for (int i = 1; i < max; i++) {
        SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i));

        maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth());
        maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight());
    }

    // Apply the new width/height Spring. This forces all the
    // components to have the same size.
    for (int i = 0; i < max; i++) {
        SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i));

        cons.setWidth(maxWidthSpring);
        cons.setHeight(maxHeightSpring);
    }

    // Then adjust the x/y constraints of all the cells so that they
    // are aligned in a grid.
    SpringLayout.Constraints lastCons = null;
    SpringLayout.Constraints lastRowCons = null;
    for (int i = 0; i < max; i++) {
        SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i));
        if (i % cols == 0) { // start of new row
            lastRowCons = lastCons;
            cons.setX(initialXSpring);
        } else { // x position depends on previous component
            cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST), xPadSpring));
        }

        if (i / cols == 0) { // first row
            cons.setY(initialYSpring);
        } else { // y position depends on previous row
            cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH), yPadSpring));
        }
        lastCons = cons;
    }

    // Set the parent's size.
    SpringLayout.Constraints pCons = layout.getConstraints(parent);
    pCons.setConstraint(SpringLayout.SOUTH,
            Spring.sum(Spring.constant(yPad), lastCons.getConstraint(SpringLayout.SOUTH)));
    pCons.setConstraint(SpringLayout.EAST,
            Spring.sum(Spring.constant(xPad), lastCons.getConstraint(SpringLayout.EAST)));
}

From source file:Main.java

private void myPaint(Component comp, Graphics g) {
    int x = comp.getX();
    int y = comp.getY();
    g.translate(x, y);/* www.j a v a 2s  .c  om*/
    cursor.translate(-x, -y);
    if (comp.contains(cursor)) {
        String cls_name = comp.getClass().getName();
        g.setColor(Color.black);
        g.drawString(cls_name, 0, 10);
    }
    if (comp instanceof Container) {
        Container cont = (Container) comp;
        for (int i = 0; i < cont.getComponentCount(); i++) {
            Component child = cont.getComponent(i);
            myPaint(child, g);
        }
    }

    cursor.translate(x, y);
    g.translate(-x, -y);
}

From source file:FocusTraversalExample.java

private void sortRecursive(Map buttons, Container container) {
    for (int i = 0; i < container.getComponentCount(); i++) {
        Component c = container.getComponent(i);
        if (c instanceof JButton) { // Found another button to sort.
            buttons.put(((JButton) c).getText(), c);
        }//from  w w w  .  ja  va 2s. c  o  m
        if (c instanceof Container) { // Found a container to search.
            sortRecursive(buttons, (Container) c);
        }
    }
}

From source file:org.datacleaner.widgets.DCFileChooser.java

private void setContainerBackground(Component component, Color bg) {
    if (component instanceof Container) {
        Container c = (Container) component;
        // drill further down the tree
        Component child = c.getComponent(0);
        if (child instanceof Container) {
            Container childContainer = (Container) child;
            if (childContainer.getComponentCount() == 1) {
                setContainerBackground(childContainer, bg);
            }//from   w w  w.j  a v a 2  s .  c  o  m
        }
    }
    component.setBackground(bg);
}

From source file:MainClass.java

private void applyOrientation(Component c, ComponentOrientation o) {
    c.setComponentOrientation(o);/*from   w w  w  .  jav  a  2  s .  co m*/

    if (c instanceof JMenu) {
        JMenu menu = (JMenu) c;
        int ncomponents = menu.getMenuComponentCount();
        for (int i = 0; i < ncomponents; ++i) {
            applyOrientation(menu.getMenuComponent(i), o);
        }
    } else if (c instanceof Container) {
        Container container = (Container) c;
        int ncomponents = container.getComponentCount();
        for (int i = 0; i < ncomponents; ++i) {
            applyOrientation(container.getComponent(i), o);
        }
    }
}

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);
    }//w  w w  .ja va2s  .  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;/*from ww  w  .ja v  a 2s .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:nl.xs4all.home.freekdb.b52reader.gui.ManyBrowsersPanelTest.java

private void assertManyBrowsersPanel(ManyBrowsersPanel manyBrowsersPanel, JWebBrowser expectedBrowser,
        boolean expectBrowserPanelVisible) {
    assertEquals(2, manyBrowsersPanel.getComponentCount());

    // The embedded browser is added to a browser panel, which is added to the manyBrowsersPanel.
    Container browserPanel = (Container) manyBrowsersPanel.getComponent(0);
    Component embeddedBrowser = browserPanel.getComponent(0);

    assertEquals(expectedBrowser, embeddedBrowser);
    assertEquals(expectBrowserPanelVisible, browserPanel.isVisible());
}

From source file:WrapperLayout.java

public Dimension preferredLayoutSize(Container arg0) {
    java.awt.Insets insets = arg0.getInsets();
    int count = arg0.getComponentCount();
    if (count > 0) {
        Dimension d = arg0.getComponent(0).getPreferredSize();
        return new Dimension(d.width + insets.left + insets.right, d.height + insets.top + insets.bottom);
    } else {//ww  w  .  j a  v a2  s .  c om
        return new Dimension(insets.left + insets.right, insets.top + insets.bottom);
    }
}

From source file:WrapperLayout.java

public Dimension minimumLayoutSize(Container arg0) {
    java.awt.Insets insets = arg0.getInsets();
    int count = arg0.getComponentCount();
    if (count > 0) {
        Dimension d = arg0.getComponent(0).getMinimumSize();
        return new Dimension(d.width + insets.left + insets.right, d.height + insets.top + insets.bottom);
    } else {/* w w w  . j  ava2  s . co m*/
        return new Dimension(insets.left + insets.right, insets.top + insets.bottom);
    }
}