3

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.

Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
Evans Belloeil
  • 2,413
  • 7
  • 43
  • 76

1 Answers1

4

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.

ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
  • Whoa, I never thought about this for the second case, I was working on something with setOnClick, this really helps me, thanks. – Evans Belloeil Jun 17 '15 at 12:07
  • 1
    Are you sure your code is working ? Because the solution for first case doesn't work for me, when I add an Item in my CheckListView, the checkbox is still empty ... – Evans Belloeil Jun 17 '15 at 13:01
  • @EvansBelloeil For the first case, the cell is selected as you mentioned in your question. If you want it checked, see the update. – ItachiUchiha Jun 17 '15 at 13:28
  • 1
    This is not working :/ even if c.getAddedSubList().get(0) return me the correct inserted item – Evans Belloeil Jun 17 '15 at 13:42
  • This works perfectly for me, can you show me what did you try and what exactly is not working? May be I didn't get your requirement correctly. You want the newly added item to be checked, right? – ItachiUchiha Jun 17 '15 at 13:44
  • 1
    I correct it with platform.runLater, but only works for first item, see my update for more – Evans Belloeil Jun 17 '15 at 14:31
  • If you are performing this action on some thread other than JavaFX application thread, then you need `Platform.runLater`, other wise there is no need for it. Though, I would suggest you to run the MCVE I have provided and check it is works as per you requirement, if not, state your requirement clearly. – ItachiUchiha Jun 17 '15 at 14:34
  • Your updated post doesn't add any details of how the data is getting added. Can you try and replicate your issue in a MCVE? – ItachiUchiha Jun 17 '15 at 14:47
  • c.getAddedSubList().get(0) always return me the same element, that's why it doesn't work ... I add elements simply with add, and sort the list every time an element is added. – Evans Belloeil Jun 17 '15 at 15:01
  • 1
    I tested it with a sorted collection, and it might be a bug (though I am still not sure). I came up with few more issues and have logged all of them in a [ticket](https://bitbucket.org/controlsfx/controlsfx/issue/553/checklistview-multiple-issues) with ControlsFX developers. You can follow it for updates. :) – ItachiUchiha Jun 19 '15 at 08:41
  • @EvansBelloeil I had the same problem with the **first case**, and `runLater` solved the problem too. Which is strange because I'm on the main thread ! –  Aug 01 '18 at 16:02