1

I am using CheckComboBox control from ControlsFX project.

But I want to create a custom rule:

When you click at Item0, then it should clean all other selections. If you click at Item0 again, it remain checked. If you select Item(X), it clean Item0 and select Item(X).

The idea is that Item0 should be the "All" Option.

enter image description here

Edit: This solution is for ControlsFX.

ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
Marckaraujo
  • 7,422
  • 11
  • 59
  • 97
  • Possible duplicate of [How to check and uncheck all items when checking or unckeck some of the items](https://stackoverflow.com/questions/41229964/how-to-check-and-uncheck-all-items-when-checking-or-unckeck-some-of-the-items) – Brad Turek Nov 20 '17 at 21:56

1 Answers1

1

I am not very familiar with ControlsFX but messing around a bit I think I found a solution to your problem. Below is a full example. I hope the comments will fill any question.

import org.controlsfx.control.CheckComboBox;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    public void start(Stage mainStage) throws Exception {


        ObservableList<String> items = FXCollections.observableArrayList();

        items.addAll(new String[] { "All", "Item 1", "Item 2", "Item 3", "Item 4" });

        CheckComboBox<String> controll = new CheckComboBox<String>(items);

        controll.getCheckModel().getCheckedItems().addListener(new ListChangeListener<String>() {
            public void onChanged(ListChangeListener.Change<? extends String> c) {

                while (c.next()) {
                    if (c.wasAdded()) {
                        if (c.toString().contains("All")) {

                            // if we call the getCheckModel().clearChecks() this will
                            // cause to "remove" the 'All' too at least inside the model.
                            // So we need to manually clear everything else
                            for (int i = 1; i < items.size(); i++) {
                                controll.getCheckModel().clearCheck(i);
                            }

                        } else {
                            // check if the "All" option is selected and if so remove it
                            if (controll.getCheckModel().isChecked(0)) {
                                controll.getCheckModel().clearCheck(0);
                            }

                        }
                    }
                }
            }
        });

        Scene scene = new Scene(controll);
        mainStage.setScene(scene);
        mainStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
JKostikiadis
  • 2,847
  • 2
  • 22
  • 34
  • this works pretty well. The only problem that I would like to prevent "All" from being unchecked if it already is checked. – Marckaraujo Oct 30 '17 at 18:39
  • I am afraid that's not possible in my opinion.The CheckComboBox doesn't provide an API to check a specific checkbox inside the CheckComboBox. Thus you can set the data model `controll.getCheckModel().check(0);` to contain the 'All" selection if the user tries to uncheck it but it will show you that the checkbox inside is unchecked. – JKostikiadis Oct 30 '17 at 23:48
  • @Marckaraujo, though `IndexedCheckModel` doesn't seem to have an API for checking a specific checkbox, [`CheckBitSetModelBase`](https://bitbucket.org/controlsfx/controlsfx/src/ddd3706a94a7f65591220047d9f6430e1b4fe13d/controlsfx/src/main/java/org/controlsfx/control/CheckBitSetModelBase.java?at=default&fileviewer=file-view-default) does and that's what `CheckComboBox` uses. Just use `.check("All");`. Same goes for `.isChecked("All");` – Brad Turek Nov 17 '17 at 22:03