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

[TableView] setFixedCellSize causes new columns to be blank

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 8u40
    • 8u20
    • javafx
    • linux, windows 7, JDK 8 update 20

      The code below shows a TableView with one column and a button to add a second column. If the button is clicked the second column is added but it doesn't display any content. If setFixedCellSize is commented out then the second column is displayed as expected.

      If the column is removed and then added again it works as expected.

      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.Button;
      import javafx.scene.control.TableColumn;
      import javafx.scene.control.TableView;
      import javafx.scene.control.cell.PropertyValueFactory;
      import javafx.scene.layout.FlowPane;
      import javafx.stage.Stage;

      public class FixedCellHeightTableTest 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.setFixedCellSize(24);
              TableColumn<Person,String> firstNameCol = new TableColumn<>("First Name");
              TableColumn<Person,String> lastNameCol = new TableColumn<>("Last Name");
              firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));
              lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));
              table.setItems(data);
              table.getColumns().addAll(firstNameCol);
              Button button = new Button("Add or Remove Column");
              button.setOnAction(actionEvent -> {
                  if (table.getColumns().size() == 1) {
                      table.getColumns().add(lastNameCol);
                  } else {
                      table.getColumns().remove(lastNameCol);
                  }
              });
              flowPane.getChildren().addAll(table, button);
              stage.show();
          }

          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:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: