Handle event on CheckListView of Controls FX

3.2k views Asked by At

I try to work with Controls FX and the Check List View component, but I have several issues on how to use it :

  • By default, cell are not selected when I add item in the CheckListView, how can I do to have it selected by default ? I think I have to use setCheckModel but I'm lost.
  • How can I handle an event when someone click on a checkBox ? I don't know what to do, because event that I handle are on the node but not on the checkBox. I don't understand how to use the eventHandler with this component.

EDIT : Here's what I do :

departureCheckListView.setItems(myListAirport.getObservableDepartureAirtport());
departureCheckListView.getItems().addListener(new ListChangeListener<String>() {
    @Override
    public void onChanged(Change<? extends String> c) {
        c.next();
        if (c.wasAdded()) {
            System.out.println(c.getAddedSubList().get(0));
            //departureCheckListView.getSelectionModel().select(c.getAddedSubList().get(0)); 
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    departureCheckListView.getCheckModel().check(c.getAddedSubList().get(0));
                }
            });
        }
    }
});

The first item that I add is checked, but the followed items.

I don't know if this could helps, but my list is sorted.

1

There are 1 answers

11
ItachiUchiha On

For your first case, use a Listener on the List of Items in the CheckListView, to check if an item is added to it or nor, then, use the getSelectionModel().select(<Item>) to select it.

checkListView.getItems().addListener(new ListChangeListener<String>() {
     @Override
     public void onChanged(Change<? extends String> c) {
         c.next();
         if (c.wasAdded()) {
             checkListView.getSelectionModel().select(c.getAddedSubList().get(0));
         }
     }
});

For the second case, use getCheckModel().getCheckedItems() to get the List of Items that have checked values. Similarly, check if a an item has been added / removed from the list.

checkListView.getCheckModel().getCheckedItems().addListener(new ListChangeListener<String>() {
    @Override
    public void onChanged(ListChangeListener.Change<? extends String> c) {
        c.next();
        if(c.wasAdded()) {
             System.out.println("Item Checked : " + c.getAddedSubList().get(0));
        } else if (c.wasRemoved()) {
             System.out.println("Item Unchecked : " + c.getRemoved().get(0));
        }
    }
});

Complete MCVE - Tested with ControlsFX - 8.40.9

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

public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        final ObservableList<String> listOfItems = FXCollections.observableArrayList();
        for (int i = 0; i <= 100; i++) {
            listOfItems.add("Item " + i);
        }
        final CheckListView<String> checkListView = new CheckListView<>(listOfItems);

        // Select the first checkListView element
        checkListView.getItems().addListener(new ListChangeListener<String>() {
            @Override
            public void onChanged(Change<? extends String> c) {
                c.next();
                if (c.wasAdded()) {
                    checkListView.getSelectionModel().select(c.getAddedSubList().get(0));
                }
            }
        });

        // On CheckBox event
        checkListView.getCheckModel().getCheckedItems().addListener(new ListChangeListener<String>() {
            @Override
            public void onChanged(ListChangeListener.Change<? extends String> c) {
                c.next();
                if(c.wasAdded()) {
                    System.out.println("Item Checked : " + c.getAddedSubList().get(0));
                } else if (c.wasRemoved()) {
                    System.out.println("Item Unchecked : " + c.getRemoved().get(0));
                }
            }
        });

    Button button = new Button("Add");
    button.setOnAction(e -> {
        checkListView.getItems().add(0, "Itachi");
            checkListView.requestFocus();
        });
        Scene scene = new Scene(new VBox(checkListView, button), 300, 275);
        primaryStage.setTitle("Welcome");
        primaryStage.setScene(scene);
        primaryStage.show();

    }
}

Update : For checking the new added item check-box, instead of selecting

Use :

checkListView.getCheckModel().check(c.getAddedSubList().get(0));

instead of

checkListView.getSelectionModel().select(c.getAddedSubList().get(0));

If you want it to be checked and at the same time selected, you can use both of them.