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

TableView focus/selection broken when adding a new column

XMLWordPrintable

      When the button in the code below is clicked, the second column is removed and a new one is added.

      If a cell in the first column is selected then after clicking the button the cursor keys can be used to move the selection around. This is what is expected.

      If however, a cell in the second column is selected then after clicking the button the cursor keys do not change the selection. The table is focused and the cell looks selected. If you click on the selected cell it doesn't give it focus. You have to click on a different cell in the table to get the select/focus working again.

      Even worse, if you click the button a couple of times (with a cell in the second column selected) and then try the cursor keys a few times (mash them) the application locks up and CPU goes to 100%.

      import javafx.application.Application;
      import javafx.beans.property.SimpleStringProperty;
      import javafx.collections.FXCollections;
      import javafx.collections.ObservableList;
      import javafx.scene.Scene;
      import javafx.scene.control.*;
      import javafx.scene.control.cell.PropertyValueFactory;
      import javafx.scene.layout.FlowPane;
      import javafx.stage.Stage;

      public class TableViewFocusTest extends Application {
          @Override
          public void start(Stage stage) throws Exception {
              FlowPane flowPane = new FlowPane();
              Scene scene = new Scene(flowPane, 500, 500);
              stage.setScene(scene);

              ObservableList<Person> data = FXCollections.observableArrayList(
                      new Person("Jacob", "Smith"),
                      new Person("Isabella", "Johnson"),
                      new Person("Ethan", "Williams"),
                      new Person("Emma", "Jones"),
                      new Person("Michael", "Brown")
              );

              TableView<Person> table = new TableView<>();
              table.getSelectionModel().setCellSelectionEnabled(true);
              table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

              TableColumn<Person,String> firstNameCol = new TableColumn<>("First Name");
              firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));
              table.getColumns().addAll(firstNameCol, create2ndColumn());
              table.setItems(data);

              Button button = new Button("Replace 2nd Column");
              button.setFocusTraversable(false);
              button.setOnAction(actionEvent -> {
                  table.getColumns().remove(1);
                  table.getColumns().add(create2ndColumn());
              });
              flowPane.getChildren().addAll(table, button);
              stage.show();
          }

          public TableColumn<Person,String> create2ndColumn() {
              TableColumn<Person,String> lastNameCol = new TableColumn<>("Last Name");
              lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));
              return lastNameCol;
          }

          public static class Person {
              private final SimpleStringProperty firstName;
              private final SimpleStringProperty lastName;
              private Person(String fName, String lName) {
                  this.firstName = new SimpleStringProperty(fName);
                  this.lastName = new SimpleStringProperty(lName);
              }
              public String getFirstName() {return firstName.get();}
              public void setFirstName(String fName) {firstName.set(fName);}
              public String getLastName() {return lastName.get();}
              public void setLastName(String fName) {lastName.set(fName);}
          }

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

            jgiles Jonathan Giles
            ndarcy Nick D'Arcy
            Votes:
            2 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: