A DESCRIPTION OF THE PROBLEM :
TableView.getSelectionModel().getSelectedItems() observable list incorrectly reports the list changes when a FilteredList is used. For example, it can report to registered listeners that 3 elements were removed while the list contained only one before the change.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the attached code and you'll notice that even though the list contained only one element, after the list is cleared, it reports that 3 elements were removed.
---------- BEGIN SOURCE ----------
package com.test;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Table extends Application {
public static void main(String[] pArgs) {launch(pArgs);}
public void start(Stage pStage) {
final BorderPane pane = new BorderPane();
final Scene scene = new Scene(pane);
pStage.setScene(scene);
TableView<String> table = new TableView<>();
ObservableList<String> list = table.getItems();
FilteredList<String> filteredList = new FilteredList<>(list);
list.add("a");
list.add("b");
list.add("c");
table.setItems(filteredList);
TableColumn<String,String> col1 = new TableColumn<>();
col1.setCellValueFactory(features -> new SimpleStringProperty(features.getValue()));
table.getColumns().setAll(col1);
pane.setCenter(table);
pStage.show();
table.getSelectionModel().select(0);
System.out.println("Elements: ");
for (String str: table.getSelectionModel().getSelectedItems()) {
System.out.println(str);
}
System.out.println();
table.getSelectionModel().getSelectedItems().addListener(
(ListChangeListener.Change<? extends String> pChange) -> {
while (pChange.next()) {
System.out.println("Removed elements: ");
for (String str : pChange.getRemoved()) {
System.out.println(str);
}
}
}
);
filteredList.setPredicate(pStr -> "a".equals(pStr));
}
}
---------- END SOURCE ----------
TableView.getSelectionModel().getSelectedItems() observable list incorrectly reports the list changes when a FilteredList is used. For example, it can report to registered listeners that 3 elements were removed while the list contained only one before the change.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the attached code and you'll notice that even though the list contained only one element, after the list is cleared, it reports that 3 elements were removed.
---------- BEGIN SOURCE ----------
package com.test;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Table extends Application {
public static void main(String[] pArgs) {launch(pArgs);}
public void start(Stage pStage) {
final BorderPane pane = new BorderPane();
final Scene scene = new Scene(pane);
pStage.setScene(scene);
TableView<String> table = new TableView<>();
ObservableList<String> list = table.getItems();
FilteredList<String> filteredList = new FilteredList<>(list);
list.add("a");
list.add("b");
list.add("c");
table.setItems(filteredList);
TableColumn<String,String> col1 = new TableColumn<>();
col1.setCellValueFactory(features -> new SimpleStringProperty(features.getValue()));
table.getColumns().setAll(col1);
pane.setCenter(table);
pStage.show();
table.getSelectionModel().select(0);
System.out.println("Elements: ");
for (String str: table.getSelectionModel().getSelectedItems()) {
System.out.println(str);
}
System.out.println();
table.getSelectionModel().getSelectedItems().addListener(
(ListChangeListener.Change<? extends String> pChange) -> {
while (pChange.next()) {
System.out.println("Removed elements: ");
for (String str : pChange.getRemoved()) {
System.out.println(str);
}
}
}
);
filteredList.setPredicate(pStr -> "a".equals(pStr));
}
}
---------- END SOURCE ----------