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

TableView focus/selection broken when adding a new column



    • Bug
    • Status: Resolved
    • P3
    • Resolution: Fixed
    • 8u20
    • 8u40
    • javafx
    • OSX, JDK 8 update 20


      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 {
          public void start(Stage stage) throws Exception {
              FlowPane flowPane = new FlowPane();
              Scene scene = new Scene(flowPane, 500, 500);

              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<>();

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

              Button button = new Button("Replace 2nd Column");
              button.setOnAction(actionEvent -> {
              flowPane.getChildren().addAll(table, button);

          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) {




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