Question

How do I get the quantity of all selected JToggleButtons in a ButtonGroup?

ButtonGroup bGroup = new ButtonGroup();
bGreen = new JToggleButton("Green");
bYellow = new JToggleButton("Yellow");
bRed = new JToggleButton("Red");

bGroup.add(bGreen);
bGroup.add(bYellow);
bGroup.add(bRed);
Was it helpful?

Solution

There are several ways. For example bGroup.getSelection().getSelectedObjects().length. You can also call getElements(), then iterate over enumeration and ask isSelected() for each button. Please refer to API doc.

OTHER TIPS

You have to add a item listener to each of the toogle

public int counter = 0;
public void itemStateChanged(ItemEvent e) {
    if(e.getStateChange() == ItemEvent.SELECTED)
    {
         counter++;
    }
    else
    {
        counter --;
    } 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top