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

[TableView] Sorting table while cell editor is active has odd behaviour

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P4 P4
    • 8u40
    • 8u40
    • javafx
    • java version "1.8.0_40-ea"
      Java(TM) SE Runtime Environment (build 1.8.0_40-ea-b07)
      Java HotSpot(TM) 64-Bit Server VM (build 25.40-b11, mixed mode)

      I notice that when you have a table with cell editing active and you sort the table by clicking on a column header, the row selection moves to the correct "post-sort" location, but the cell editor remains in place (and will now edit the content of a different cell than before). Here is a small test program to illustrate - double-click the cell containing "Williams" to begin editing, then click on the "last name" column header. Notice that the row selection follows "Ethan Williams" as you would expect, but the editing cell remains in position and now edits "Emma Jones":

      import javafx.application.Application;
      import javafx.beans.property.SimpleBooleanProperty;
      import javafx.beans.property.SimpleStringProperty;
      import javafx.collections.FXCollections;
      import javafx.collections.ObservableList;
      import javafx.collections.transformation.SortedList;
      import javafx.scene.Group;
      import javafx.scene.Scene;
      import javafx.scene.control.TableColumn;
      import javafx.scene.control.TableView;
      import javafx.scene.control.cell.PropertyValueFactory;
      import javafx.scene.control.cell.TextFieldTableCell;
      import javafx.scene.layout.BorderPane;
      import javafx.stage.Stage;
       
      public class SortedTableView extends Application {

          public static class Person {
              
              private final SimpleStringProperty firstName;
              private final SimpleStringProperty lastName;
              private final SimpleStringProperty email;
              private final SimpleBooleanProperty highlighted;
           
              public Person(String fName, String lName, String email) {
                  this.firstName = new SimpleStringProperty(fName);
                  this.lastName = new SimpleStringProperty(lName);
                  this.email = new SimpleStringProperty(email);
                  this.highlighted = new SimpleBooleanProperty(false);
              }
           
              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 String getEmail() {
                  return email.get();
              }
           
              public void setEmail(String fName) {
                  email.set(fName);
              }
                  
              public boolean isHighlighted() {
                  return highlighted.get();
              }
                  
              public void setHighlighted(boolean highlighted) {
                  this.highlighted.set(highlighted);
              }
                  
              public SimpleStringProperty firstNameProperty() {
                  return firstName;
              }

              public SimpleStringProperty lastNameProperty() {
                  return lastName;
              }

              public SimpleStringProperty emailProperty() {
                  return email;
              }
                  
              public SimpleBooleanProperty highlightedProperty() {
                  return highlighted;
              }
          }
          private TableView<Person> table;
          
          private final ObservableList<Person> data =
                  FXCollections.observableArrayList(
                  new Person("Jacob", "Smith", "jacob.smith@example.com"),
                  new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
                  new Person("Ethan", "Williams", "ethan.williams@example.com"),
                  new Person("Emma", "Jones", "emma.jones@example.com"),
                  new Person("Michael", "Brown", "michael.brown@example.com"));
       
          @Override
          public void start(Stage stage) {
              Scene scene = new Scene(new Group());
              stage.setTitle("SortedTableView");
              stage.setWidth(450);
              stage.setHeight(550);
              BorderPane pane = new BorderPane();
              table = new TableView<Person>();
              TableColumn<Person, String> col1 = new TableColumn<>("first name");
              col1.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
              col1.setCellFactory(TextFieldTableCell.forTableColumn());
              TableColumn<Person, String> col2 = new TableColumn<>("last name");
              col2.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
              col2.setCellFactory(TextFieldTableCell.forTableColumn());
              TableColumn<Person, String> col3 = new TableColumn<>("e-mail");
              col3.setCellValueFactory(new PropertyValueFactory<Person, String>("email"));
              col3.setCellFactory(TextFieldTableCell.forTableColumn());
              table.getColumns().addAll(col1, col2, col3);
              table.setEditable(true);
              SortedList<Person> sortedData = new SortedList<Person>(data);
              table.setItems(sortedData);
              sortedData.comparatorProperty().bind(table.comparatorProperty());
              pane.setCenter(table);
              scene.setRoot(pane);
              stage.setScene(scene);
              stage.show();
          }
          
          public static void main(String[] args) {
              launch(args);
          }
       
      }

      I would expect the mouse-click on the column header to close the editing cell, then do the row sorting.

            jgiles Jonathan Giles
            dgilbertjfx David Gilbert (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: