简体   繁体   中英

Keep ListView with Checkboxes synchronized with a list of Strings

I want to create a simple ListView and synchronize its selected items with strings contained in a list. I have figured out I can use the method setCellFactory(), I update the String list when items are selected/unselected, but I don't know how to do the opposite, updating the ListView items selection when the list of strings change.

So far I have:

ListView<String> selectedAttributes = new ListView<>();
String[] toppings = {"Cheese", "Pepperoni", "Black Olives"};
        listViewAttributes.getItems().addAll(toppings);
        listViewAttributes.setCellFactory(CheckBoxListCell.forListView(new Callback<String, ObservableValue<Boolean>>() {
            @Override
            public ObservableValue<Boolean> call(String item) {
                BooleanProperty observable = new SimpleBooleanProperty();
                observable.addListener((obs, wasSelected, isNowSelected) -> {
                    if (isNowSelected) {
                        selectedAttributes.add(item);
                    } else {
                        selectedAttributes.remove(item);
                    }
                    System.out.println(selectedAttributes.size());

                });
                return observable;
            }
        }));

This makes the list selectedAttributes elements updated when the ListView items are checked/unchecked, now I want to update the ListView items selection when the List content is changed.

How can I do that?

Assuming selectedAttributes is an ObservableSet , (or an ObservableList where you carefully check you do not add the same item multiple times) add

observable.set(selectedAttributes.contains(item));
selectedAttributes.addListener((ListChangeListener.Change<? extends String> c) -> 
    observable.set(selectedAttributes.contains(item)));

Here is a SSCCE with this approach:

import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableSet;
import javafx.collections.SetChangeListener;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.cell.CheckBoxListCell;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.util.Callback;

public class ListViewWithCheckBoxes extends Application {

    @Override
    public void start(Stage primaryStage) {
        ListView<String> toppingsList = new ListView<>();
        String[] toppings = {"Cheese", "Tomato Sauce", "Pepperoni", "Black Olives"};
        toppingsList.getItems().addAll(toppings);

        ObservableSet<String> selectedToppings = FXCollections.observableSet();

        toppingsList.setCellFactory(CheckBoxListCell.forListView(new Callback<String, ObservableValue<Boolean>>() {
            @Override
            public ObservableValue<Boolean> call(String item) {
                BooleanProperty observable = new SimpleBooleanProperty();
                observable.addListener((obs, wasSelected, isNowSelected) -> {
                    if (isNowSelected) {
                        selectedToppings.add(item);
                    } else {
                        selectedToppings.remove(item);
                    }
                    System.out.println(selectedToppings.size());

                });

                observable.set(selectedToppings.contains(item));
                selectedToppings.addListener((SetChangeListener.Change<? extends String> c) -> 
                    observable.set(selectedToppings.contains(item)));

                return observable;
            }
        }));


        // example of a button that changes what's selected in the list
        // This selects "Cheese" and "Tomato Sauce" and deselects everything else
        Button justCheese = new Button("Just a cheese pizza");
        justCheese.setOnAction(e -> {
            selectedToppings.clear();
            selectedToppings.add("Cheese");
            selectedToppings.add("Tomato Sauce");
        });

        BorderPane root = new BorderPane(toppingsList);
        root.setTop(justCheese);
        BorderPane.setMargin(justCheese, new Insets(5));

        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

The way you have of doing things (with a separate list representing the selected items) gets a bit convoluted. At this point, it's probably easier just to define an appropriate model class for the items in the list which has the observable property:

public class Topping {

    private final String name ;

    private final BooleanProperty selected = new SimpleBooleanProperty();

    public Topping(String name) {
        this.name = name ;
    }

    public BooleanProperty selectedProperty() {
        return selected ;
    }

    public final boolean isSelected() {
        return selectedProperty().get();
    }

    public final void setSelected(boolean selected) {
        selectedProperty().set(selected);
    }

    public String getName() {
        return name ;
    }

    @Override
    public String toString() {
        return getName();
    }
}

Then the remaining code is much easier:

ListView<Topping> listView = new ListView<>();
List<Topping> toppings = Arrays.asList(
    new Topping("Cheese"), 
    new Topping("Pepperoni") , 
    new Topping("Black Olives"));
listView.getItems().addAll(toppings);

listView.setCellFactory(CheckBoxListCell.forListView(Topping::selectedProperty));

And now you can select items externally to the check box in the UI simply by calling, eg

for (Topping topping : toppings) {
    topping.setSelected(true);
}

Here is a SSCCE with this approach:

import java.util.Arrays;
import java.util.List;

import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.cell.CheckBoxListCell;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class ListViewWithCheckBoxes extends Application {

    @Override
    public void start(Stage primaryStage) {
        ListView<Topping> toppingsList = new ListView<>();
        Topping cheese = new Topping("Cheese");
        Topping tomSauce = new Topping("Tomato Sauce");
        Topping pepperoni = new Topping("Pepperoni");
        Topping blackOlives = new Topping("Black Olives");

        toppingsList.getItems().addAll(cheese, tomSauce, pepperoni, blackOlives);


        toppingsList.setCellFactory(CheckBoxListCell.forListView(Topping::selectedProperty));

        // example of a button that changes what's selected in the list
        // This selects "Cheese" and "Tomato Sauce" and deselects everything else

        Button justCheese = new Button("Just a cheese pizza");
        List<Topping> cheesePizzaToppings = Arrays.asList(cheese, tomSauce);

        justCheese.setOnAction(e -> toppingsList.getItems().forEach(
                topping -> topping.setSelected(cheesePizzaToppings.contains(topping))));

        BorderPane root = new BorderPane(toppingsList);
        root.setTop(justCheese);
        BorderPane.setMargin(justCheese, new Insets(5));

        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static class Topping {

        private final String name ;

        private final BooleanProperty selected = new SimpleBooleanProperty();

        public Topping(String name) {
            this.name = name ;
        }

        public BooleanProperty selectedProperty() {
            return selected ;
        }

        public final boolean isSelected() {
            return selectedProperty().get();
        }

        public final void setSelected(boolean selected) {
            selectedProperty().set(selected);
        }

        public String getName() {
            return name ;
        }

        @Override
        public String toString() {
            return getName();
        }
    }


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

Using for loop to compare String item to list by equals() method,if item is equal then set observable true using below code.

ListView<String> listViewAttributes = new ListView<>();

        List<String> selectedAttributes = new ArrayList<>();
        selectedAttributes.add("Pepperoni");

        String[] toppings = { "Cheese", "Pepperoni", "Black Olives" };
        listViewAttributes.getItems().addAll(toppings);
        listViewAttributes
                .setCellFactory(CheckBoxListCell.forListView(new Callback<String, ObservableValue<Boolean>>() {
                    @Override
                    public ObservableValue<Boolean> call(String item) {
                        BooleanProperty observable = new SimpleBooleanProperty();
                        for (int i = 0; i < selectedAttributes.size(); i++) {
                            if (item.equals(selectedAttributes.get(i))) {
                                observable.set(true);
                            }
                        }
                        observable.addListener((obs, wasSelected, isNowSelected) -> {
                            if (isNowSelected) {
                                selectedAttributes.add(item);
                            } else {
                                selectedAttributes.remove(item);
                            }
                            System.out.println(selectedAttributes.size());

                        });
                        return observable;
                    }
                }));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM