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

[TableView] selectedItemProperty is fired when we select different cells in the same row, with the event's new value being null

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P2 P2
    • 8u40
    • 8u40
    • javafx
    • windows 7 jdk 8u40 64 bit

      This bug does not affect version 8u25, I reproduced it on 8u40 build b16
      steps to reproduce:
      1- create a table with at least 2 columns and 1 row
      2- Add a changelistener to its selectedItemProperty
      3- click on the first cell, at this point selectedItemProperty takes the value of the first row.
      3- click on the same row, second cell
      At this point selectedItemProperty is changed to null
      4- click back on the first cell
      At this point the changelistener is fired twice and selectedItemProperty ends up with null

      The following is a sample test code:

      import javafx.application.Application;
      import javafx.beans.property.ReadOnlyObjectWrapper;
      import javafx.beans.value.ChangeListener;
      import javafx.beans.value.ObservableValue;
      import javafx.scene.Scene;
      import javafx.scene.control.TableColumn;
      import javafx.scene.control.TableColumn.CellDataFeatures;
      import javafx.scene.control.TableView;
      import javafx.stage.Stage;
      import javafx.util.Callback;

      public class ss extends Application {

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

      @Override
      public void start(Stage primaryStage) throws Exception {
      TableView<Person> table = new TableView<>();
      table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
      table.setTableMenuButtonVisible(true);
      TableColumn<Person, String> column = new TableColumn<>("first");
      column.setCellValueFactory(new Callback<CellDataFeatures<Person, String>, ObservableValue<String>>() {
      public ObservableValue<String> call(CellDataFeatures<Person, String> p) {
      return new ReadOnlyObjectWrapper(p.getValue().getFirstName());
      }
      });
      table.getColumns().add(column);
      column = new TableColumn<>("last");
      column.setCellValueFactory(new Callback<CellDataFeatures<Person, String>, ObservableValue<String>>() {
      public ObservableValue<String> call(CellDataFeatures<Person, String> p) {
      return new ReadOnlyObjectWrapper(p.getValue().getLastName());
      }
      });
      table.getColumns().add(column);
      table.getItems().addAll(new Person("peter", "M."), new Person("john", "doe"));
      table.getSelectionModel().selectedItemProperty().addListener(new SelectionListener());
      table.getSelectionModel().selectedIndexProperty().addListener(new indexListener());
      primaryStage.setScene(new Scene(table));
      primaryStage.show();
      }
      }

      class SelectionListener implements ChangeListener<Person> {
      @Override
      public void changed(ObservableValue<? extends Person> observable, Person oldValue, Person newValue) {
      if (newValue == null) {
      System.out.println("null selection");
      }
      else {
      System.out.println("item selected");
      }
      }
      }

      class indexListener implements ChangeListener<Number> {
      @Override
      public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
      if (newValue == null) {
      System.out.println("null index");
      }
      else {
      System.out.println(newValue);
      }
      }
      }

      class Person {
      String first, last;

      public Person(String first, String last) {
      super();
      this.first = first;
      this.last = last;
      }

      public String getFirstName() {
      return first;
      }

      public Object getLastName() {
      return last;
      }
      }

            jgiles Jonathan Giles
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: