Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8093320

[TableView] Multi-Select TableView: Bound list on selected items is not in sync

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 8u40
    • 8u40
    • javafx
    • Windows 7/8.1

      Following example shows the issue:

      Whenever you bind the selectionModel's selectedItem list to another observable list they will eventually contain different/duplicate items. especially when you hit CTL+A and deselect you will notice that the printout sizes don't match.

      ---------------------------------------------------------
      package sandbox;

      import javafx.application.Application;
      import javafx.beans.binding.Bindings;
      import javafx.beans.property.IntegerProperty;
      import javafx.beans.property.SimpleIntegerProperty;
      import javafx.beans.property.SimpleStringProperty;
      import javafx.beans.property.StringProperty;
      import javafx.collections.FXCollections;
      import javafx.collections.ListChangeListener;
      import javafx.collections.ObservableList;
      import javafx.scene.Scene;
      import javafx.scene.control.SelectionMode;
      import javafx.scene.control.TableColumn;
      import javafx.scene.control.TableView;
      import javafx.scene.control.cell.PropertyValueFactory;
      import javafx.scene.layout.BorderPane;
      import javafx.stage.Stage;

      public class TableTest extends Application {
      @Override
      public void start(Stage primaryStage) {
      BorderPane root = new BorderPane();
      Scene scene = new Scene(root, 600, 400);

      ObservableList<Person> personList = FXCollections.observableArrayList();
      Person person1 = new Person("name1", 12);
      Person person2 = new Person("name2", 33);
      Person person3 = new Person("name3", 44);
      Person person4 = new Person("name4", 44);
      Person person5 = new Person("name5", 44);
      Person person6 = new Person("name6", 44);

      personList.setAll(person1, person2, person3, person4, person5, person6);

      TableView<Person> personTable = new TableView<>();
      personTable.setEditable(true);
      personTable.setTableMenuButtonVisible(true);
      personTable.setItems(personList);

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

      TableColumn<Person, String> nameColumn = new TableColumn<>("Name");
      nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));

      TableColumn<Person, Integer> ageColumn = new TableColumn<>("Age");
      ageColumn.setCellValueFactory(new PropertyValueFactory<>("age"));

      personTable.getColumns().addAll(nameColumn, ageColumn);

      root.setCenter(personTable);

      primaryStage.setScene(scene);
      primaryStage.show();

      ObservableList<Person> _selectedPersons = FXCollections.observableArrayList();

      personTable.getSelectionModel().getSelectedItems().addListener((ListChangeListener<Person>) change -> {
      System.err.println("selectionModel: " + change.getList().size());
      });

      _selectedPersons.addListener((ListChangeListener<Person>) change -> {
      System.err.println("selectedPersons: " + change.getList().size());
      });

      Bindings.bindContent(_selectedPersons, personTable.getSelectionModel().getSelectedItems());
      }

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

      public static class Person {
      private StringProperty nameProperty = new SimpleStringProperty();
      private IntegerProperty ageProperty = new SimpleIntegerProperty();

      public Person(String name, Integer age) {
      setName(name);
      setAge(age);
      }

      public StringProperty nameProperty() {
      return nameProperty;
      }

      public void setName(String name) {
      nameProperty.set(name);
      }

      public String getName() {
      return nameProperty.get();
      }

      public IntegerProperty ageProperty() {
      return ageProperty;
      }

      public void setAge(Integer age) {
      ageProperty.set(age);
      }

      public Integer getAge() {
      return ageProperty.get();
      }

      @Override
      public String toString() {
      return "" + getName() + "/" + getAge();
      }
      }
      }

            jgiles Jonathan Giles
            smorandijfx Stefano Morandi (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: