get selected button from ButtonGroup - Java Swing

Java examples for Swing:JButton

Description

get selected button from ButtonGroup

Demo Code


//package com.java2s;

import java.util.Enumeration;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;

public class Main {
    /**//w  w w.j a  v a  2s  .  c  om
     * get selected button from ButtonGroup
     * @param bg
     * @return
     */
    public static AbstractButton getSelectedButton(ButtonGroup bg) {
        Enumeration<AbstractButton> elements = bg.getElements();
        while (elements.hasMoreElements()) {
            AbstractButton button = elements.nextElement();
            if (button.isSelected())
                return button;
        }
        return null;
    }
}

Related Tutorials