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

TableView shows content in empty cells

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P3 P3
    • 8
    • 8
    • javafx
    • Win 7 x64
      JavaFX 8 - b106

      The program below works fine if the line value.setCellFactory(NUMBER_CELL_FACTORY); is commented out.

      If it is included however, the TableView behaves weirdly (on JavaFX 8 b106) - if you simply copy the code, launch it and click a few times on the `+` button, you will see content appearing in empty rows at the bottom of the table, and the colours are not as they should be.

      Note: adding if (empty) { setText(null); setGraphic(null); } solves the first issue (content in empty cells) but the colours are still not applied correctly.

          import javafx.application.Application;
          import javafx.beans.property.DoubleProperty;
          import javafx.beans.property.SimpleDoubleProperty;
          import javafx.scene.Scene;
          import javafx.scene.control.Button;
          import javafx.scene.control.TableCell;
          import javafx.scene.control.TableColumn;
          import javafx.scene.control.TableView;
          import javafx.scene.control.cell.PropertyValueFactory;
          import javafx.scene.layout.AnchorPane;
          import javafx.scene.layout.VBox;
          import javafx.scene.paint.Color;
          import javafx.stage.Stage;
          import javafx.util.Callback;
          
          public class TestFX extends Application {
          
              @Override
              public void start(final Stage stage) throws Exception {
                  MyTable table = new MyTable();
          
                  Scene scene = new Scene(table);
          
                  stage.setScene(scene);
                  stage.show();
              }
          
              public static void main(String[] args) {
                  launch(TestFX.class);
              }
          
              static class MyTable extends AnchorPane {
          
                  private static final Callback NUMBER_CELL_FACTORY = (p) -> new NumberTableCell<>();
          
                  private final Button addRow = new Button("+");
                  private final TableView<Item> table = new TableView<>();
          
                  MyTable() {
                      super();
                      initTableView();
                      addRow.setOnMouseClicked((e) -> addItem());
                      VBox vbox = new VBox();
                      vbox.getChildren().addAll(addRow, table);
                      getChildren().add(vbox);
                  }
          
                  public void addItem() {
                      table.getItems().add(new Item());
                  }
          
                  private void initTableView() {
                      TableColumn<Item, Double> value = new TableColumn<>("Value");
                      value.setCellValueFactory(new PropertyValueFactory<>("value"));
                      value.setCellFactory(NUMBER_CELL_FACTORY);
          
                      table.getColumns().add(value);
                      table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
                  }
              }
          
              public static class Item {
                  private final DoubleProperty value = new SimpleDoubleProperty();
          
                  public DoubleProperty valueProperty() {
                      return value;
                  }
              }
          
              static class NumberTableCell<T> extends TableCell<T, Double> {
                  private final Color fill = Color.RED;
          
                  @Override
                  protected void updateItem(Double item, boolean empty) {
                      super.updateItem(item, empty);
                      if (empty) return;
                      setText(String.valueOf(item));
                      setTextFill(fill);
                  }
              }
          }

            jgiles Jonathan Giles
            ytallecjfx Yann Le Tallec (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: