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

/**
 * Obtains selected text//from  w w w  .j a va2 s .c o m
 *
 * @param component the component that is an EditorPane or some of its
 *      subcomponents is an EditorPane
 * @return current caret position
 */
public static String getSelectedText(Container component) {
    if (component.getClass().getName().indexOf("EditorPane") >= 0) {
        try {
            java.lang.reflect.Method caretGetter = component.getClass().getMethod("getSelectedText",
                    new Class[] {});
            Object result = caretGetter.invoke(component, new Object[] {});
            if (result == null) {
                return null;
            }
            return (String) result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Method invocation exception caught");
        }
    }

    for (int i = 0; i < component.getComponentCount(); i++) {
        Component childComponent = component.getComponent(i);
        if (childComponent instanceof JComponent) {
            return getSelectedText((JComponent) childComponent);
        }
    }

    return null;
}

From source file:SpringBox.java

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread./*  www .ja  v  a 2  s  .  c om*/
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("SpringBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Set up the content pane.
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new SpringLayout());

    //Add the buttons.
    contentPane.add(new JButton("Button 1"));
    contentPane.add(new JButton("Button 2"));
    contentPane.add(new JButton("Button 3"));
    contentPane.add(new JButton("Long-Named Button 4"));
    contentPane.add(new JButton("5"));

    //Lay out the buttons in one row and as many columns
    //as necessary, with 6 pixels of padding all around.
    SpringUtilities.makeCompactGrid(contentPane, 1, contentPane.getComponentCount(), 6, 6, 6, 6);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

From source file:SpringBox.java

/**
 * Create the GUI and show it. For thread safety, this method should be
 * invoked from the event-dispatching thread.
 *//*from   ww  w  .j ava 2 s  .  c o  m*/
private static void createAndShowGUI() {
    //Make sure we have nice window decorations.
    JFrame.setDefaultLookAndFeelDecorated(true);

    //Create and set up the window.
    JFrame frame = new JFrame("SpringBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Set up the content pane.
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new SpringLayout());

    //Add the buttons.
    contentPane.add(new JButton("Button 1"));
    contentPane.add(new JButton("Button 2"));
    contentPane.add(new JButton("Button 3"));
    contentPane.add(new JButton("Long-Named Button 4"));
    contentPane.add(new JButton("5"));

    //Lay out the buttons in one row and as many columns
    //as necessary, with 6 pixels of padding all around.
    SpringUtilities.makeCompactGrid(contentPane, 1, contentPane.getComponentCount(), 6, 6, 6, 6);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

/**
 * Fills up the given ArrayList with all Components, matching to the given
 * class file, found downwards the given Container. The Container should be
 * a JRootPane for example./*w  ww. j  a  v a  2 s  . c  o  m*/
 *
 * @param container
 *            Container that should be searched for The Component
 * @param recursive
 *            An empty ArrayList which will be filled up or null if a new
 *            ArrayList should be returned.
 * @return An array of the type specified with the className will be
 *         returned. If className is <code>null</code> an array over
 *         {@link Component} will be returned.
 */
private static ArrayList<Component> getAllComponentsRecursive(Container container,
        Class<? extends Component> classNames[], ArrayList<Component> recursive) {
    //is there no class name defined, just setup the default value.
    if (classNames == null || classNames.length == 0) {
        classNames = new Class[] { Component.class };
    }

    // No ArrayList, just create a new one.
    if (recursive == null) {
        // recursive = new Object[0];
        recursive = new ArrayList<>(100);
    }

    // No Container, nothing to do.
    if (container == null) {
        return recursive;
    }

    // Search for the component which is an instance of the Class specified
    // with the className parameter
    for (int i = 0; i < container.getComponentCount(); i++) {
        try {
            Component comp = container.getComponent(i);

            if (comp instanceof Container) {
                getAllComponentsRecursive((Container) comp, classNames, recursive);
            }

            for (int j = 0; j < classNames.length; j++) {
                if (classNames[j] == null || classNames[j].isInstance(comp)) {
                    recursive.add(comp);
                    break;
                }
            }
        } catch (Exception e) {
            // container.getComponent(i); can fail if it was removed.
        }
    }
    return recursive;
}

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

private void applyOrientation(Component c, ComponentOrientation o) {
    c.setComponentOrientation(o);//w  w  w.j  a va2  s  .  c  o 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:Main.java

private void myPaint(Component comp, Graphics g) {
    int x = comp.getX();
    int y = comp.getY();
    g.translate(x, y);//from w w  w. j av a2 s .c o m
    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);
        }//w w w  . j  a  v a  2  s  . c  om
        if (c instanceof Container) { // Found a container to search.
            sortRecursive(buttons, (Container) c);
        }
    }
}

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  ww  .j av a  2  s .  com
}

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 ww .j  a  v a  2  s .c  om
        }
    }
    component.setBackground(bg);
}