Get text for selected CheckBox in JavaFx ListView


Question

I've created an example project to learn how to use checkboxes in a ListView.

When I select a checkbox, I want to print out its value (i.e. the text value on the checkbox label).

What happens now is when I select a checkbox e.g. Row 5, it is printed out:
Row 5

And when I continue with selecting the checkbox for Row 7, the following is printed out:
Row 5
Row 7

So, all print out's looks like this in whole:
Row5
Row5 <-- should not print out here!
Row7

As you can see, the problem is when I select one checkbox, ALL checkboxes that are selected are printed out.

The same if I uncheck Row 7, I want to print out that ONLY Row 7 has been unchecked. But now, all unchecked checkboxes are printed out.

package listviewtest;

    import java.net.URL;
    import java.util.ResourceBundle;
    import javafx.beans.value.ChangeListener;
    import javafx.beans.value.ObservableValue;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.control.Button;
    import javafx.scene.control.CheckBox;
    import javafx.scene.control.Label;
    import javafx.scene.control.ListCell;
    import javafx.scene.control.ListView;
    import javafx.scene.control.cell.CheckBoxListCell;
    import javafx.util.Callback;
    import javafx.util.StringConverter;

    public class SubGuiController implements Initializable {
        @FXML private ListView listView;
        @FXML private Button btnUnselectAll;
        @FXML private Button btnSelectAll;
        @FXML private Label lblSelected;

        /**
         * Initializes the controller class.
         */
        @Override
        public void initialize(URL url, ResourceBundle rb) {
            final ObservableList<CheckBoxHandler> data = FXCollections.observableArrayList();

            for(int i=0; i<10; i++) {
                data.add(new CheckBoxHandler(false, "Row " + i));
            }

                ChangeListener<Boolean> listener = new ChangeListener<Boolean>() {

                    @Override
                    public void changed(ObservableValue<? extends Boolean> observableValue, Boolean oldValue, Boolean selected) {

                        // In this loop, we get all selected/unselected checkboxes...
                        for (CheckBoxHandler cbh : data) {

                            if (selected) {
                                if (cbh.getSelected()) {
                                    System.out.println("selected: " + cbh.getCheckBoxText() + ": " + cbh.getSelected());
                                }
                            }
                            else if (!selected) {
                                if (!cbh.getSelected()) {
                                    System.out.println("unselected: " + cbh.getCheckBoxText());
                                }
                            }
                        }
                    }
                };

                for(CheckBoxHandler cbh: data) {
                    cbh.selectedProperty().addListener(listener);
                }

            listView.setItems(data);
    }
1
0
6/10/2014 11:44:19 AM

Accepted Answer

Add a different listener to each CheckBoxHandler:

for (final CheckBoxHandler cbh : data) {
    cbh.selectedProperty().addListener(new ChangeListener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> obs, Boolean wasSelected, Boolean isSelected) {
            if (isSelected) {
                System.out.println("selected: "+cbh.getCheckBoxText());
            } else {
                System.out.println("unselected: "+cbh.getCheckBoxText());
            }
        }
    });
}
0
6/10/2014 11:53:44 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon