Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;

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

public class Main {
    public static List<JButton> getListenedButtonsFor(Container c) {
        List<JButton> filter = null;
        List<JButton> result = new ArrayList<JButton>();
        searchFor(c, JButton.class, filter = new ArrayList<JButton>(), null);
        for (JButton button : filter) {
            if (button.getActionListeners().length > 0) {
                result.add(button);
            }
        }
        return result;
    }

    private static void searchFor(Container c, Class cls, List list, String name) {
        if (!isEmpty(c.getComponents())) {
            for (Component comp : c.getComponents()) {
                if (cls.isInstance(comp)
                        && (name == null ? true : (comp.getName() != null && comp.getName().matches(name)))) {
                    list.add(comp);
                } else if (comp instanceof Container) {
                    searchFor((Container) comp, cls, list, name);
                }
            }
        }
    }

    public static boolean isEmpty(Object o) {
        return o == null || (o instanceof String && o.toString().trim().equals(""));
    }
}