0

How to untoggle jtogglebuttons that belongs to ButtonGroups on second click? What listener should I use for this? Thanks!

JtoggleButton toggleButton;

toggleButton.addActionListener? toggleButton.addChangeListener?

Philip Morris
  • 459
  • 1
  • 9
  • 26
  • you have to 1. (easier) search for custom ButtonGroup, because this is implemented only for JRadioButtons, to rest of ButtonComponents (JButton - JCeckBox - JMenuXxx) is omitted, 2. (direct, simpler) to hold JToggleButtons in array, each event from Item/ActionListener to loop inside by toggling with setSelected – mKorbel Feb 27 '15 at 20:06

2 Answers2

0
public class CustomButtonGroup extends ButtonGroup {
  @Override
  public void setSelected(ButtonModel model, boolean selected) {
    if (selected) {
      super.setSelected(model, selected);
    } else {
      clearSelection();
    }
  }
}
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
Philip Morris
  • 459
  • 1
  • 9
  • 26
  • 1
    This is simply not required, the `ButtonGroup` does this by design – MadProgrammer Feb 27 '15 at 21:43
  • It solved my problem. Try making 2 JToggleButtons and place them in a ButtonGroup. By default, the JtoggleButton can only be untoggled if you select the other button. I wanted to untoggle it by clicking the same JTogglebutton and this solved the problem – Philip Morris Mar 02 '15 at 09:31
0

How to untoggle jtogglebuttons that belongs to ButtonGroups on second click? What listener should I use for this?

None, the ButtonGroup will take care of it automatically, that's the point. The ButtonGroup will only allow a single button to be selected within the group at a time.

enter image description here

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public JavaApplication243() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            ButtonGroup bg = new ButtonGroup();
            JToggleButton btn1 = new JToggleButton("One");
            JToggleButton btn2 = new JToggleButton("Two");

            bg.add(btn1);
            bg.add(btn2);

            add(btn1);
            add(btn2);
        }

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366