JavaFX ListView Multiple Selection


Question

I want to select multiple items from ListView. It would react on mouse click. I tried with this:

selectedLogsList.addAll(logsListView.getSelectionModel().getSelectedItems());

but it was giving me duplicates. So I tried this:

logsListView.getSelectionModel().selectedItemProperty().addListener(
        new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> ov, 
                String old_val, String new_val) {
                    if(selectedLogsList.contains(new_val)) {
                        selectedLogsList.remove(new_val);
                    } else {
                        selectedLogsList.add(new_val);
                    }
        }
    });

I would also like that, if cell is selected, it should be marked as such (different background color) Thanks!

1
7
6/11/2014 8:43:02 AM

Accepted Answer

If you just want to know which items are selected have a look at my example. Have a look at the setOnMouseClicked() method..

    package application;

    import javafx.application.Application;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.event.Event;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.ListView;
    import javafx.scene.control.SelectionMode;
    import javafx.scene.layout.Pane;
    import javafx.stage.Stage;


    public class Main extends Application {
        @Override
        public void start(Stage primaryStage) {
            try {
                Pane  root = new Pane();
                Scene scene = new Scene(root,600,600);
                scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

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

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





                listView.setItems(list);

                list.add("item1");
                list.add("item2");
                list.add("item3");

                listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);


                listView.setOnMouseClicked(new EventHandler<Event>() {

                    @Override
                    public void handle(Event event) {
                        ObservableList<String> selectedItems =  listView.getSelectionModel().getSelectedItems();

                        for(String s : selectedItems){
                            System.out.println("selected item " + s);
                        }

                    }

                });


                list.add("item4");  

                root.getChildren().add(listView);

                primaryStage.setScene(scene);
                primaryStage.show();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }

        public static void main(String[] args) {
            launch(args);
        }
    }
14
6/11/2014 9:25:42 AM

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