-1

When I use an application like Eclipse or Word, I occasionally see a JCheckBox that is neither selected nor deselected- it has a sort of 'minus' icon: An example of this box in Microsoft Word

What does this mean? Is there a purpose for it, and if so, how do I achieve it in Java?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
RobotKarel314
  • 417
  • 3
  • 14

2 Answers2

1

Usually, this kind of checkbox is used in a tree view (a checkbox which contains a subset of checkboxes) to indicate that it has been partially selected.

For example, when you are in the SDK page in Android Studio, you have a bunch of checkboxes for the different versions of the SDK, and each version is composed of the sources, the build tools, etc. When you select all the elements inside a version, the version's checkbox will have a normal tick, however if you select only the sources inside a version, the checkbox of that version will be a minus or some other icons. You could also find this in installers when there are optional features, like Notepad++ and many others.

As for implementing it in Java, I don't think there are any built-in solutions in Swing, you will either have to write it yourself like in the post that camickr put in the comments, or you will have to use other libraries like JavaFX or other third-parties.

Community
  • 1
  • 1
OliPro007
  • 415
  • 6
  • 17
0

I think Swing is not supported to build the UI. But this code have the same function. Check this:

    myCheckBox.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                //Check all Checkbox below
                childCheckbox1.setSelected(true);
                childCheckbox2.setSelected(true);
            } else {
                //Uncheck all Checkbox below
                childCheckbox1.setSelected(false);
                childCheckbox2.setSelected(false);
            }
        }
    });
Satria Winarah
  • 55
  • 1
  • 10
  • This is only the behaviour that the root checkbox have on the child checkboxes (if you check the root all childs get checked), however this doesn't allow the OP to create the partial selection ("minus") icon... – OliPro007 May 31 '16 at 04:14
  • The partial selection is only appear at first right. After you check it, and uncheck it again, it will not appear again. Is that just a UI design? – Satria Winarah May 31 '16 at 04:27
  • Well normally it should always appear if there is a partial selection. You check `myCheckBox`, it checks all the childs, you have a full selection. If you don't check `myCheckBox` but you check `childCheckBox1` **or** `childCheckBox2`, `myCheckBox` should have the partial selection icon and if you check `childCheckBox1` **and** `childCheckBox2`, `myCheckBox` should have the full selection icon. – OliPro007 May 31 '16 at 04:41
  • Okay I see. I think we can still custom it. We can create a function which check all checkbox if they is selected or not. The function always call when the child checkbox selected or deselected. And the function change the icon of parent checkbox. But, will be better if @RobotKarel314 search other library for this. – Satria Winarah May 31 '16 at 04:53
  • It would be better indeed if the OP searches for another library than Swing, but the solution you suggest is basically the answer that camickr linked in his comment. The OP needs to override the default class and the default painting behaviour to add the third icon, however it seems to work correctly only on Windows. – OliPro007 May 31 '16 at 05:12
  • That is the solution – Satria Winarah May 31 '16 at 05:54