Example usage for java.awt Container getComponentCount

List of usage examples for java.awt Container getComponentCount

Introduction

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

Prototype

public int getComponentCount() 

Source Link

Document

Gets the number of components in this panel.

Usage

From source file:Main.java

public static Point positionToClickPoint(Container component, int caretPosition, Container invokedIn) {
    if (component == null) {
        return null;
    }/*from w  ww  . j  av a 2s  .  c om*/

    //System.err.println("Checking: " + component.getClass().getName());
    if (component.getClass().getName().indexOf("EditorPane") >= 0) {
        try {
            java.lang.reflect.Method pointGetter = component.getClass().getMethod("modelToView",
                    new Class[] { Integer.TYPE });
            Rectangle rec = (Rectangle) pointGetter.invoke(component,
                    new Object[] { new Integer(caretPosition) });
            //System.err.println("Before: " + (int)rec.getY());
            // FIXME: somehow it fails here to convert point from scrollable component
            Point point = SwingUtilities.convertPoint(component, (int) rec.getX(), (int) rec.getY() + 10,
                    invokedIn);
            // FIXME: ugly hack :(
            if (point.getY() > 1024) {
                point = new Point((int) point.getX(), 250);
            }
            //System.err.println("After: " + (int)point.getY());
            return point;
        } catch (Exception e) {
            System.err.println("Method invocation exception caught");
            e.printStackTrace();

            //FIXME: BUG
            return null;
            //throw new RuntimeException("Method invocation exception caught");
        }
    }

    for (int i = 0; i < component.getComponentCount(); i++) {
        java.awt.Component childComponent = component.getComponent(i);
        if (childComponent instanceof javax.swing.JComponent) {
            Point point = positionToClickPoint((javax.swing.JComponent) childComponent, caretPosition,
                    invokedIn);
            if (point != null) {
                return point;
            }
        }
    }

    return null;
}

From source file:TableLayout.java

private void loadComponents(Container parent) {
    ncomponents = parent.getComponentCount();

    // If we haven't allocated the right sized array for each column yet, do so now.
    // Note that the number of columns is fixed, but the number of rows is not know
    // and could in the worst case be up the number of components. Unfortunately this
    // means we need to allocate quite big arrays, but the alternative would require
    // complex multiple passes as we try to work out the effect of row spanning.
    if (components[0] == null || components[0].length < ncomponents) {
        for (int i = 0; i < ncols; ++i)
            components[i] = new Component[ncomponents];
    }//from   w  w  w.  ja  va 2 s . co m
    // Nullify the array
    for (int i = 0; i < ncols; ++i) {
        for (int j = 0; j < components[i].length; ++j)
            components[i][j] = null;
    }

    // fill the matrix with components, taking row/column spanning into account
    int row = 0, col = 0;
    for (int i = 0; i < ncomponents; ++i) {
        // get the next component and its options
        Component comp = parent.getComponent(i);
        TableOption option = (TableOption) options.get(comp);
        if (option == null)
            option = defaultOption;

        // handle options to force us to column 0 or to skip columns
        if (option.forceColumn >= 0) {
            if (col > option.forceColumn)
                ++row;
            col = option.forceColumn;
        }
        col += option.skipColumns;
        if (col >= ncols) {
            ++row;
            col = 0;
        }

        // skip over any cells that are already occupied
        while (components[col][row] != null) {
            ++col;
            if (col >= ncols) {
                ++row;
                col = 0;
            }
        }

        // if using colspan, will we fit on this row?
        if (col + option.colSpan > ncols) {
            ++row;
            col = 0;
        }

        // for now, fill all the cells that are occupied by this component
        for (int c = 0; c < option.colSpan; ++c)
            for (int r = 0; r < option.rowSpan; ++r)
                components[col + c][row + r] = comp;

        // advance to the next cell, ready for the next component
        col += option.colSpan;
        if (col >= ncols) {
            ++row;
            col = 0;
        }
    }

    // now we know how many rows there are
    if (col == 0)
        nrows = row;
    else
        nrows = row + 1;

    // now we've positioned our components we can thin out the cells so
    // we only remember the top left corner of each component
    for (row = 0; row < nrows; ++row) {
        for (col = 0; col < ncols; ++col) {
            Component comp = components[col][row];
            for (int r = row; r < nrows && components[col][r] == comp; ++r) {
                for (int c = col; c < ncols && components[c][r] == comp; ++c) {
                    if (r > row || c > col)
                        components[c][r] = null;
                }
            }
        }
    }
}

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);/*w w w  .j av a  2s  . c om*/
        }
    }
}

From source file:CircleLayoutTest.java

public void layoutContainer(Container parent) {
    setSizes(parent);/* w w w  .j av  a 2s. com*/

    // 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 v a  2  s  .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;//from w w w  .j a v a  2 s. c  o  m
        }
        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  www.  java  2 s . c  o m*/
        }
    }

From source file:game.Clue.ClueGameUI.java

public void mouseReleased(MouseEvent e) {

    System.out.println("Mouse clicked/released on chosen room" + e.getSource().toString());

    if (player == null) {
        return;/*ww  w .  j a v a 2  s  .  c o m*/
    }

    //validate move by sending move request to Server before allowing move
    int valid = 1;

    player.setVisible(false);

    Component c = jPanel5.findComponentAt(e.getX(), e.getY());

    if (c instanceof JLabel) {
        Container parent = c.getParent();
        if (parent.getComponentCount() == 0) {
            System.out.println("------");
            //parent.remove(0);
            parent.add(player);

        } else {
            System.out.println("%%%%%");
            parent.add(player);
            parent.validate();
            //return;
        }

    } else {
        Container parent = (Container) c;
        if (parent.getComponentCount() == 1) {
            System.out.println("*****");
            return;
        } else {
            System.out.println("&&&&&");
            parent.add(player);
        }

    }
    if (valid != 0) {
        System.out.println("Player move allowed" + " x:" + e.getX() + "y: " + e.getY());
        System.out.println("Previous x:" + previous_room_x + "  Y:" + previous_room_y);
        player.setLocation(previous_room_x, previous_room_y);
        // player.setLocation(e.getX(),e.getY());
    } else {

    }
    player.setHorizontalAlignment(SwingConstants.CENTER);
    player.setVisible(true);

}

From source file:ca.canucksoftware.ipkpackager.IpkPackagerView.java

private void disableNewFolderButton(Container c) {
    int len = c.getComponentCount();
    for (int i = 0; i < len; i++) {
        Component comp = c.getComponent(i);
        if (comp instanceof JButton) {
            JButton b = (JButton) comp;
            Icon icon = b.getIcon();
            if (icon != null && (icon == UIManager.getIcon("FileChooser.newFolderIcon")
                    || icon == UIManager.getIcon("FileChooser.upFolderIcon")))
                b.setEnabled(false);/*www.  j  av a 2  s  .  co m*/
        } else if (comp instanceof Container) {
            disableNewFolderButton((Container) comp);
        }
    }
}

From source file:org.eclipse.jubula.rc.swing.components.AUTSwingHierarchy.java

/**
 * Returns all descendents of the given <code>component</code>
 * @param c a <code>component</code> value
 * @return a <code>collection</code> of the component's descendents or an
 * empty <code>collection</code> if nothing was found or <code>c</code> is null.
 *///from   w  w  w. ja va 2  s . c  o  m
private Collection getComponents(Component c) {
    if (c instanceof Container) {
        Container cont = (Container) c;
        List list = new ArrayList();
        list.addAll(Arrays.asList(cont.getComponents()));
        if (c instanceof JMenu) {
            list.add(((JMenu) c).getPopupMenu());
        } else if (c instanceof Window) {
            list.addAll(Arrays.asList(((Window) c).getOwnedWindows()));
        } else if (c instanceof JDesktopPane) {
            // add iconified frames, which are otherwise unreachable
            // for consistency, they are still considerered children of
            // the desktop pane.
            int count = cont.getComponentCount();
            for (int i = 0; i < count; i++) {
                Component child = cont.getComponent(i);
                if (child instanceof JInternalFrame.JDesktopIcon) {
                    JInternalFrame frame = ((JInternalFrame.JDesktopIcon) child).getInternalFrame();
                    if (frame != null) {
                        list.add(frame);
                    }
                }
            }
        }
        return list;
    }
    // an empty ArrayList
    return new ArrayList();
}