In a ListView with multi-selection, when selecting items with shift-up from the last one, we get the following change events:
--- select the last item (for example by clicking on the last item) OK ---
{ [One] replaced by [Four] at 0, }
-- select both last items (with Shift-up keys OK ---
{ [Three] added at 0, }
-- select the three last items (with Shift-up keys KO ---
{ [Four] removed at 3, } and { [Two, Three] added at 0, }
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ListViewChangeEvent extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
ObservableList<String> items = FXCollections.observableArrayList("One", "Two", "Three", "Four");
ListView<String> listView = new ListView<String>(items);
listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
listView.getSelectionModel().getSelectedItems().addListener(new ListChangeListener<String>() {
@Override
public void onChanged(Change<? extends String> change) {
System.out.println(change);
}
});
VBox vBox = new VBox();
vBox.getChildren().add(listView);
Scene scene = new Scene(vBox);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
--- select the last item (for example by clicking on the last item) OK ---
{ [One] replaced by [Four] at 0, }
-- select both last items (with Shift-up keys OK ---
{ [Three] added at 0, }
-- select the three last items (with Shift-up keys KO ---
{ [Four] removed at 3, } and { [Two, Three] added at 0, }
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ListViewChangeEvent extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
ObservableList<String> items = FXCollections.observableArrayList("One", "Two", "Three", "Four");
ListView<String> listView = new ListView<String>(items);
listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
listView.getSelectionModel().getSelectedItems().addListener(new ListChangeListener<String>() {
@Override
public void onChanged(Change<? extends String> change) {
System.out.println(change);
}
});
VBox vBox = new VBox();
vBox.getChildren().add(listView);
Scene scene = new Scene(vBox);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}