1

I have a few JToggleButton in a ButtonGroup using ItemListener's itemStateChanged Event to detect when a button is selected/unselected. It does not work in the case where I click on a button itself to select/unselect it. how do I solve this issue? If I use ActionListener, when the ActionEvent is invoked, the Button's selection state(isSelected()) always says true.

ButtonGroup buttonGroup = new ButtonGroup();
for(int i=0;i<toggleButtons.length;i++){
    buttonGroup.add(toggleButtons[i]);
}
for(int i=0;i<toggleButtons.length;i++){
    final int  j=i;
    toggleButtons[i].addItemListener(new ItemListener(){
    public void itemStateChanged(ItemEvent e) {
         JToggleButton item=(JToggleButton)e.getSource();
      if(e.getStateChange()==ItemEvent.SELECTED){
          System.err.println(j+" selected!!!! "+item.getText());
      } else if(e.getStateChange()==ItemEvent.DESELECTED){
          System.err.println(j+" unselected!!!! "+item.getText());
      }
    }
    });
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user121196
  • 30,032
  • 57
  • 148
  • 198
  • Do not add your `JToggleButton` to the ButtonGroup, that's it, then you can have the behaviour you looking for, though you have to do the added work manually, which was previously accomplished by your `ButtonGroup` :-) – nIcE cOw May 03 '12 at 06:15

2 Answers2

4

You cannot actively deselect a button in a ButtonGroup, you have to select another button, but when a new button is selected, the previous is deselected, and two events are triggered, one for the selected and one for the deselected item.

For example:

public class FrameTest extends JFrame {

    public FrameTest init() {
        ItemListener listener = new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                ((JToggleButton)e.getSource()).setText("" + e.getStateChange());
            }
        };
        setLayout(new GridLayout(2,1));
        ButtonGroup bg = new ButtonGroup();
        // button 1
        JToggleButton jtb = new JToggleButton("Button");
        jtb.addItemListener(listener);
        bg.add(jtb);
        add(jtb);
        // button 2
        jtb = new JToggleButton("Button");
        jtb.addItemListener(listener);
        bg.add(jtb);
        add(jtb);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        pack();
        return this;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new FrameTest().init();
            }
        });
    }
}

produces:

not selected any yet Selected upper button Selected bottom button

MByD
  • 135,866
  • 28
  • 264
  • 277
  • Sharetbut: if I want to actively deselect a button without selecting other buttons, what's the best way to go about it? – user121196 May 02 '12 at 21:58
  • At button group you can have only one button selected at a time, if you want to deselect it, it's just like deselecting all, you can call the `clearSelection` method of `ButtonGroup`. – MByD May 02 '12 at 22:06
2

If you want really to use ButtonGroup, then you have look for Custom(ized) ButtonGroup by Darryl Burke

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • would the Customized ButtonGroup work for deselecting by clicking the Button itself? It doesn't say on the documentation. Are there other approaches other than ButtonGroup? – user121196 May 02 '12 at 21:56
  • @user121196 right but valid only for JRadioButton or JCheckBox, but then is possile to select only one element from ButtonGroup – mKorbel May 02 '12 at 22:04