get Swing Selected Button Text from ButtonGroup - Java Swing

Java examples for Swing:JButton

Description

get Swing Selected Button Text from ButtonGroup

Demo Code


//package com.java2s;
import java.util.Enumeration;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;

public class Main {
    /**//from www.ja v a2 s. c om
     * @param buttonGroup
     * @return the text corresponding to the button selected
     */
    public static String getSelectedButtonText(ButtonGroup buttonGroup) {
        for (Enumeration<AbstractButton> buttons = buttonGroup
                .getElements(); buttons.hasMoreElements();) {
            AbstractButton button = buttons.nextElement();

            if (button.isSelected()) {
                return button.getText();
            }
        }

        return null;
    }
}

Related Tutorials