3

Why can't I set button model for JCheckBox?

The following code works and draws a window with one single check box in the center. Check box is operational:

public class JCheckButton_Test {
public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            ButtonModel buttonModel = new DefaultButtonModel();

            JCheckBox checkBox = new JCheckBox();
            checkBox.setText("Check Box");
            //checkBox.setModel(buttonModel);

            JPanel controlPanel = new JPanel();
            controlPanel.add(checkBox);

            JFrame frame = new JFrame();

            frame.add(controlPanel, BorderLayout.CENTER);

            frame.pack();
            frame.setSize(640, 200);

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    });
}
}

But if I add a model to the box (uncomment line) check box become non-operational (does not change if clicked).

Why?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385

2 Answers2

4
// this is more than just a standard button..
ButtonModel buttonModel = new JToggleButton.ToggleButtonModel();
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • And how to know check box is checked (from the model)? – Suzan Cioc Feb 20 '13 at 18:22
  • Ask a new question after reading about [`ToggleButtonModel`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JToggleButton.ToggleButtonModel.html). – Andrew Thompson Feb 20 '13 at 18:27
  • let me cite you an API about `ToggleButtonModel`: "The ToggleButton model". End of citate. – Suzan Cioc Feb 20 '13 at 22:03
  • There are methods below that, complete with words. – Andrew Thompson Feb 21 '13 at 01:34
  • Yes, but all that words also say nothing about what property reflects check box checked state. For example, description of `isSelected()` method is "Checks if the button is selected." This is what can be seen from method name. No any word that "selected" means "checked". – Suzan Cioc Feb 21 '13 at 14:47
2

Cause it is the default button model implementation for buttons and reacts to your actions as button. If You still want to use ButtonModel then You should implement check box behaviour for it. For example You can use the following implementation

......
 final ButtonModel buttonModel = new DefaultButtonModel();
 buttonModel.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        buttonModel.setSelected(!buttonModel.isSelected());
    }
 });
......
Arsen Alexanyan
  • 3,061
  • 5
  • 25
  • 45
  • It will work with any allowable model implementation You provide. The problem is that the model doesn't know how to react to advanced user actions, but it still handles events from user actions, So You have to implement that functionality via event handling. – Arsen Alexanyan Feb 21 '13 at 04:51
  • I was thinking "view" should interact with "model" directly. – Suzan Cioc Feb 21 '13 at 14:50
  • It was implemented with MVC pattern, which says that view is changing the model via controller not directly. Read about MVC pattern – Arsen Alexanyan Feb 21 '13 at 15:07