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

Changing TableView selection via table.getSelectionModel().select(item) causes NULL to be selected first

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Cannot Reproduce
    • Affects Version/s: 8u20
    • Fix Version/s: 8u40
    • Component/s: javafx
    • Labels:
    • Environment:

      Windows 7 64-bit
      Java 8u20

      Description

      Programatically changing the selection in a TableView results in two calls to the selectedItemProperty's ChangeListener. First the selection is changed to null, then it is changed to the desired value. This is inconsistent with the events set when the user selects a new value via the GUI.
      Using either select(int index) or select(T obj) methods of the SelectionModel reproduce the issue.


      Press the "Select Next Item" button in this test app to reproduce:



      package tableselectionissue;

      import javafx.application.Application;
      import javafx.beans.property.SimpleStringProperty;
      import javafx.beans.property.StringProperty;
      import javafx.collections.FXCollections;
      import javafx.collections.ObservableList;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      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 TableSelectionIssue extends Application {

      TableView<Blob> table;
      ObservableList<Blob> items = FXCollections.observableArrayList();

      @Override
      public void start(Stage primaryStage) {
      Button selectNextBtn = new Button("Select Next Item");
      selectNextBtn.setOnAction(e -> selectNextItem());
      table = new TableView<>(items);
      table.getSelectionModel().setCellSelectionEnabled(false);
      table.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
      TableColumn<Blob, String> nameColumn = new TableColumn<>("Name");
      nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
      table.getColumns().add(nameColumn);
      for (char i = 'A'; i < 'Z'; i++) {
      items.add(new Blob("Item "+i));
      }

      table.getSelectionModel().selectedItemProperty().addListener((src, oldVal, newVal) -> {
      if (newVal == null) {
      // This shouldn't happen unless we ctrl-click to deselect a node
      System.out.println("**** SelectedItem is NULL! ****");
      } else {
      System.out.println("SelectedItem is "+newVal.getName());
      };
      });

      BorderPane root = new BorderPane();
      root.setTop(selectNextBtn);
      root.setCenter(table);
      Scene scene = new Scene(root, 300, 250);
      primaryStage.setTitle("TableView Selection Issue");
      primaryStage.setScene(scene);
      primaryStage.show();
      }

      private void selectNextItem() {
      int index = table.getSelectionModel().getSelectedIndex();
      index = (index+1) % items.size();
      Blob item = items.get(index);
      table.getSelectionModel().select(item);
      }

      /**
      * @param args the command line arguments
      */
      public static void main(String[] args) {
      launch(args);
      }

      public static class Blob {
      SimpleStringProperty name = new SimpleStringProperty();
      public Blob(String name) {
      this.name = new SimpleStringProperty();
      this.name.set(name);
      }
      public String getName() { return name.get(); }
      public StringProperty nameProperty() { return name; }
      }
      }

        Attachments

          Activity

            People

            Assignee:
            jgiles Jonathan Giles
            Reporter:
            swpalmer Scott Palmer
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported: