get Selected Swing Button Elements - Java Swing

Java examples for Swing:JButton

Description

get Selected Swing Button Elements

Demo Code


//package com.java2s;
import java.awt.Component;
import java.awt.Container;

import java.util.Enumeration;
import java.util.Vector;
import javax.swing.AbstractButton;

public class Main {
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static Enumeration getSelectedElements(Container container) {
        Vector selections = new Vector();
        Component components[] = container.getComponents();
        for (int i = 0, n = components.length; i < n; i++) {
            if (components[i] instanceof AbstractButton) {
                AbstractButton button = (AbstractButton) components[i];
                if (button.isSelected()) {
                    selections.addElement(button.getText());
                }// w w w .  java  2  s. c om
            }
        }
        return selections.elements();
    }
}

Related Tutorials