title: JavaFX: How to refresh table?
body: <p>I have problem with refresh rows styles in JavaFX TableView.</p>
  
  <blockquote>
    <p>java version "1.8.0_51"</p>
    
    <p>Java(TM) SE Runtime Environment (build 1.8.0_51-b16)</p>
    
    <p>Java HotSpot(TM) Server VM (build 25.51-b03, mixed mode)</p>
  </blockquote>
  
  <p>Logic:</p>
  
  <ol>
  <li>Load data to tableView.</li>
  <li>Set new styles to rows by setRowFactory.</li>
  <li>Load new data to table.</li>
  <li>Refresh styles for new table rows. (Not working for me.)</li>
  </ol>
  
  <p>How to reload styles for rows?</p>
  
  <p><a href="https://i.stack.imgur.com/hCLt0.png" rel="noreferrer"><img src="https://i.stack.imgur.com/hCLt0.png" alt=""></a></p>
  
  <p>My code snippet:</p>
  
  <pre><code>import javafx.application.Application;
  import javafx.beans.property.SimpleIntegerProperty;
  import javafx.beans.property.SimpleStringProperty;
  import javafx.collections.FXCollections;
  import javafx.collections.ObservableList;
  import javafx.event.ActionEvent;
  import javafx.event.EventHandler;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.scene.control.TableColumn;
  import javafx.scene.control.TableRow;
  import javafx.scene.control.TableView;
  import javafx.scene.control.cell.PropertyValueFactory;
  import javafx.scene.layout.VBox;
  import javafx.scene.paint.Color;
  import javafx.stage.Stage;
  import javafx.util.Callback;
  
  public class RowFactoryAndOptimisationDemo extends Application {
  
          VBox root;
  
          public ObservableList&lt;RFDDomain&gt; data = FXCollections.observableArrayList();
          public TableView&lt;RFDDomain&gt; tableView = new TableView&lt;RFDDomain&gt;();
  
      @Override
      public void start(Stage stage) throws Exception {
                  root = new VBox();
          root.autosize();
          Scene scene = new Scene(root, Color.LINEN);
  
          stage.setTitle("Row Factory Demo");
          stage.setWidth(500);
          stage.setHeight(300);
          stage.setScene(scene);
          stage.show();
  
          configureTable();
      }
  
      public static void main(String[] args) {
              Application.launch(args);
      }
  
          private void recheck_table() {
              tableView.setRowFactory(new Callback&lt;TableView&lt;RFDDomain&gt;, TableRow&lt;RFDDomain&gt;&gt;() {
              @Override
              public TableRow&lt;RFDDomain&gt; call(TableView&lt;RFDDomain&gt; paramP) {
                      return new TableRow&lt;RFDDomain&gt;() {
  
                              @Override
                              protected void updateItem(RFDDomain paramT, boolean paramBoolean) {
  
                                  super.updateItem(paramT, paramBoolean);
                                  if (!isEmpty()) {
                                      String style = "-fx-control-inner-background: #007F0E;"
                              + "-fx-control-inner-background-alt: #007F0E;";
                                      setStyle(style);
                                  }
                              }
                      };
              }
      });
          }
  
  
  
      @SuppressWarnings("unchecked")
      private void configureTable() {
          int id =1;
          for (int i = 1; i &lt;= 1; i++) {
              data.add(new RFDDomain(id++,"First Row", "This is for check.", 1));
              data.add(new RFDDomain(id++,"Second Row", null, 2));
              data.add(new RFDDomain(id++,"Third Row", "This is for check.", 3));
              data.add(new RFDDomain(id++,"Fourth Row", "dil", 4));
          }
  
          tableView.setItems(data);
                  recheck_table();
  
  
          TableColumn&lt;RFDDomain, Integer&gt; column0 = new TableColumn&lt;RFDDomain, Integer&gt;("Id");
          column0.setCellValueFactory(new PropertyValueFactory&lt;RFDDomain, Integer&gt;("id"));
  
          TableColumn&lt;RFDDomain, String&gt; column1 = new TableColumn&lt;RFDDomain, String&gt;("Title");
          column1.setCellValueFactory(new PropertyValueFactory&lt;RFDDomain, String&gt;("name"));
  
          TableColumn&lt;RFDDomain, String&gt; column2 = new TableColumn&lt;RFDDomain, String&gt;("Description");
          column2.setCellValueFactory(new PropertyValueFactory&lt;RFDDomain, String&gt;("description"));
  
          TableColumn&lt;RFDDomain, Number&gt; column3 = new TableColumn&lt;RFDDomain, Number&gt;("Status");
          column3.setPrefWidth(55);
          column3.setCellValueFactory(new PropertyValueFactory&lt;RFDDomain, Number&gt;("status"));
  
          TableColumn&lt;RFDDomain, String&gt; column4 = new TableColumn&lt;RFDDomain, String&gt;("Action");
          column4.setCellValueFactory(new PropertyValueFactory&lt;RFDDomain, String&gt;("name"));
  
          tableView.getColumns().addAll(column0, column1, column2, column3, column4);
  
          this.root.getChildren().add(tableView);
  
                  Button button1 = new Button("Load new Data");
  
                  button1.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
                      @Override public void handle(ActionEvent e) {
                          data.clear();
                          data.removeAll(data);
                          data.add(new RFDDomain(1,"First Row", "This is for check.", 1));
                          data.add(new RFDDomain(2,"Second Row", null, 2));
                          tableView.setItems(data);
                          recheck_table();
                      }
                  });
  
                  this.root.getChildren().add(button1);
  
  
      }
  
      /**
       * Domain Model for this demo.
       */
      public class RFDDomain {
          private SimpleIntegerProperty id = new SimpleIntegerProperty();
          private SimpleStringProperty name = new SimpleStringProperty();
          private SimpleStringProperty description = new SimpleStringProperty();
          private SimpleIntegerProperty status = new SimpleIntegerProperty();
  
          public RFDDomain(int id,String name, String desc, int status) {
              this.id.set(id);
              this.name.set(name);
              this.description.set(desc);
              this.status.set(status);
          }
  
          public int getId() {
              return id.get();
          }
  
          public SimpleIntegerProperty idProperty() {
              return id;
          }
  
          public String getDescription() {
              return description.get();
          }
  
          public SimpleStringProperty descriptionProperty() {
              return description;
          }
  
          public String getName() {
              return name.get();
          }
  
          public SimpleStringProperty nameProperty() {
              return name;
          }
  
          public int getStatus() {
              return status.get();
          }
  
          public SimpleIntegerProperty statusProperty() {
              return status;
          }
      }
  }
  </code></pre>