Java JButton Settings getAllButtons(Component topComponent)

Here you can find the source of getAllButtons(Component topComponent)

Description

Returns an array with all JButton components contained in a top component.

License

Open Source License

Parameter

Parameter Description
topComponent The top component to scan.

Return

An list with all JButton components.

Declaration

public static List<JButton> getAllButtons(Component topComponent) 

Method Source Code


//package com.java2s;
/*/*from w w  w . ja v  a  2 s .  c om*/
 * Copyright (C) 2015 Miquel Sas
 * 
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */

import java.awt.Component;
import java.awt.Container;

import java.util.ArrayList;

import java.util.List;

import javax.swing.JButton;

public class Main {
    /**
     * Returns an array with all JButton components contained in a top component.
     * 
     * @param topComponent The top component to scan.
     * @return An list with all JButton components.
     */
    public static List<JButton> getAllButtons(Component topComponent) {
        List<Component> components = getAllComponents(topComponent);
        List<JButton> buttons = new ArrayList<>();
        for (Component component : components) {
            if (component instanceof JButton) {
                buttons.add((JButton) component);
            }
        }
        return buttons;
    }

    /**
     * Returns the list of all buttons included in the list ob objects.
     * 
     * @param objects The list of objects.
     * @return The list of buttons.
     */
    public static List<JButton> getAllButtons(List<Object> objects) {
        List<JButton> buttons = new ArrayList<>();
        for (Object object : objects) {
            if (object instanceof JButton) {
                buttons.add((JButton) object);
            }
        }
        return buttons;
    }

    /**
     * Returns the list of all components contained in a component and its subcomponents.
     * 
     * @return The list of components.
     * @param parent The parent component.
     */
    public static List<Component> getAllComponents(Component parent) {
        List<Component> list = new ArrayList<>();
        fillComponentList(parent, list);
        return list;
    }

    /**
     * Returns the array of all components contained in a component and its subcomponents.
     * 
     * @param parent The parent component.
     * @param clazz The class to filter components.
     * @return The array of components.
     */
    public static List<Component> getAllComponents(Component parent, Class<?> clazz) {
        return getAllComponents(parent, new Class[] { clazz });
    }

    /**
     * Returns the list of all components contained in a component and its subcomponents.
     * 
     * @param parent The parent component.
     * @param classes an array of possible classes.
     * @return The list of components.
     */
    public static List<Component> getAllComponents(Component parent, Class<?>[] classes) {
        List<Component> list = new ArrayList<>();
        List<Component> components = getAllComponents(parent);
        for (Component component : components) {
            for (int j = 0; j < classes.length; j++) {
                if (classes[j].isInstance(component)) {
                    list.add(component);
                }
            }
        }
        return list;
    }

    /**
     * Fills the array list with the all the components contained in the parent component and its sub-components.
     * 
     * @param list An <code>ArrayList</code>.
     * @param cmp The parent component.
     */
    public static void fillComponentList(Component cmp, List<Component> list) {
        list.add(cmp);
        if (cmp instanceof Container) {
            Container cnt = (Container) cmp;
            for (int i = 0; i < cnt.getComponentCount(); i++) {
                fillComponentList(cnt.getComponent(i), list);
            }
        }
    }

    /**
     * Returns the component with the given name contained in the top component, or null if it does not contain a
     * component with that name.
     * 
     * @param topComponent The top component.
     * @param name The name of the component to search.
     * @return The component with the name or null.
     */
    public static Component getComponent(Component topComponent, String name) {
        List<Component> components = getAllComponents(topComponent);
        for (Component component : components) {
            if (component.getName() != null && component.getName().equals(name)) {
                return component;
            }
        }
        return null;
    }
}

Related

  1. findButtonComponents(Container container, String label)
  2. findDescendantButtonWithText(Container root, String desiredText)
  3. fixButtonUI(AbstractButton button)
  4. formatButtonFromAction(Action a)
  5. getAbstractButton(Container owner, String text)
  6. getbtnButton(String iconpath, String rolloverIconpath, String pressIconpath)
  7. getButton(Container c, String text)
  8. getButton(Container container, String text)
  9. getButton(String displayText, ActionListener actionListener)