JavaFX ListView set selection model to multiple selection
import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.ComboBox; import javafx.scene.control.ContentDisplay; import javafx.scene.control.Label; import javafx.scene.control.ListView; import javafx.scene.control.ScrollPane; import javafx.scene.control.SelectionMode; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; public class Main extends Application { protected ObservableList<String> items = FXCollections.observableArrayList("China", "Japan", "Korea", "India", "Malaysia", "Vitnam"); protected ListView<String> lv = new ListView<>(items); protected ComboBox<String> cbo = new ComboBox<>(); protected Label lblSelectedItems = new Label("Selected items are "); @Override//from w w w. j a v a 2 s .co m public void start(Stage primaryStage) { // Set combo box properties cbo.getItems().addAll("SINGLE", "MULTIPLE"); cbo.setValue("SINGLE"); // Create a label and set its content display Label lblSelectionMode = new Label("Choose Selection Mode:", cbo); lblSelectionMode.setContentDisplay(ContentDisplay.RIGHT); // Set defaut list view as single lv.getSelectionModel().setSelectionMode(SelectionMode.SINGLE); // Create and register the handlers cbo.setOnAction(e -> { setMode(); setText(); }); lv.getSelectionModel().selectedItemProperty().addListener(ov -> { setMode(); setText(); }); // Place nodes in the pane BorderPane pane = new BorderPane(); pane.setTop(lblSelectionMode); pane.setCenter(new ScrollPane(lv)); pane.setBottom(lblSelectedItems); pane.setAlignment(lblSelectionMode, Pos.CENTER); // Create a scene and place it in the stage Scene scene = new Scene(pane, 268, 196); primaryStage.setTitle("java2s.com"); primaryStage.setScene(scene); primaryStage.show(); } /** Set list view selection mode */ private void setMode() { if (cbo.getValue().equals("SINGLE")) lv.getSelectionModel().setSelectionMode(SelectionMode.SINGLE); else lv.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); } /** Set label text to list view selected items */ private void setText() { String items = "Selected items are "; for (String i : lv.getSelectionModel().getSelectedItems()) items += i + " "; lblSelectedItems.setText(items); } }