3

I have this JCheckBoxMenuItem as a field:

private JCheckBoxMenuItem chckbxmntmDisableSending = new JCheckBoxMenuItem("Disable Sending");

I need to find out whether it is checked or not some time later, when doing something else (pressing the send button, basically). How do I get the value of this?

I've googled 'check value of JCheckBoxMenuItem', 'get boolean JCheckBoxMenuItem', others. I've also looked at the documentation, which says something about getState(), but I am under the impression that getState() only gives you whether it is selected by the mouse or not.

How do you get the current boolean value of a JCheckbox menu item? That is, whether it is checked or not?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
ifly6
  • 5,003
  • 2
  • 24
  • 47
  • Perhaps you should have a read of [How to Use Buttons, Check Boxes, and Radio Buttons](http://docs.oracle.com/javase/tutorial/uiswing/components/button.html) – MadProgrammer Jul 16 '15 at 02:51
  • 1
    And perhaps you should have read his question before posting a RTFM answer… since his question is NOT answered there. – geowar Oct 13 '17 at 21:50

2 Answers2

2

Straight from the JCheckBoxMenu Javadoc

Either isSelected/setSelected or getState/setState can be used to determine/specify the menu item's selection state. The preferred methods are isSelected and setSelected, which work for all menus and buttons. The getState and setState methods exist for compatibility with other component sets.

To determine when it changes state, add an ItemListener to your JCheckBoxMenuItem via addItemListener.

Josh M
  • 11,611
  • 7
  • 39
  • 49
  • So, you mean, add the ItemListener, make a boolean field, and have the ItemListener lambda expression basically say `if (field) { field = false; } else { field = true; }`? – ifly6 Jul 16 '15 at 02:49
  • The OP (and I) don't care if it's selected or not… we want to know if the checkmark is visible or not… getState & isSelected return true if it was selected... as in "just clicked on by the mouse"... so they're always true inside an ActionListener (since it's called when the mouse is click on that object). Totally worthless in determining if an item is check marked or not. – geowar Oct 13 '17 at 21:57
1

According to Javadocs, the isSelected() method will also return a boolean value for its state.

deezy
  • 1,480
  • 1
  • 11
  • 22
  • Wouldn't that boolean be whether it is selected by the mouse? Or no? – ifly6 Jul 16 '15 at 02:50
  • @ifly6 It returns the state of the `JCheckBoxMenuItem`'s selected state, regardless of how it was set – MadProgrammer Jul 16 '15 at 02:52
  • The isSelected() method is inherited from AbstractButton. Here is some more info if you want: http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#isSelected() – deezy Jul 16 '15 at 02:53
  • And, by `selected`, you mean whether it is selected by the mouse? – ifly6 Jul 16 '15 at 02:54
  • 2
    @ifly6 *"And, by selected, you mean whether it is selected by the mouse?"* In as it will return `true` if it has been "checked" or `false` when it's not. How that's achieved is irrelevent – MadProgrammer Jul 16 '15 at 03:03
  • 1
    Then how does `isSelected()` work for a JButton? You can't check that button. – ifly6 Jul 16 '15 at 03:10
  • `isSelected()` should be used for toggle buttons such as JCheckBox. – deezy Jul 16 '15 at 03:16
  • So basically the getState()/isSelected() methods are worthless in an ActionListener for a JCheckBoxMenuItem because they'll always return true... because the mouse "selected" this item in order to click it… What the OP (and I) want to know is if the checkmark is displayed or not. – geowar Oct 13 '17 at 21:53