2015-08-15 97 views
4

我在JavaFX TableView中刷新行样式有问题。JavaFX:如何刷新表格?

Java版本 “1.8.0_51”

的Java(TM)SE运行时环境(建立1.8.0_51-B16)

的HotSpot的Java(TM)服务器VM(25.51建设-B03,混合模式)

逻辑:

  1. 加载数据到的tableView。
  2. 通过setRowFactory将新样式设置为行。
  3. 将新数据加载到表中。
  4. 刷新新表格行的样式。 (不适用于我)

如何重新加载行的样式?

我的代码片段:

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<RFDDomain> data = FXCollections.observableArrayList(); 
     public TableView<RFDDomain> tableView = new TableView<RFDDomain>(); 

    @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<TableView<RFDDomain>, TableRow<RFDDomain>>() { 
      @Override 
      public TableRow<RFDDomain> call(TableView<RFDDomain> paramP) { 
        return new TableRow<RFDDomain>() { 

          @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 <= 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<RFDDomain, Integer> column0 = new TableColumn<RFDDomain, Integer>("Id"); 
     column0.setCellValueFactory(new PropertyValueFactory<RFDDomain, Integer>("id")); 

     TableColumn<RFDDomain, String> column1 = new TableColumn<RFDDomain, String>("Title"); 
     column1.setCellValueFactory(new PropertyValueFactory<RFDDomain, String>("name")); 

     TableColumn<RFDDomain, String> column2 = new TableColumn<RFDDomain, String>("Description"); 
     column2.setCellValueFactory(new PropertyValueFactory<RFDDomain, String>("description")); 

     TableColumn<RFDDomain, Number> column3 = new TableColumn<RFDDomain, Number>("Status"); 
     column3.setPrefWidth(55); 
     column3.setCellValueFactory(new PropertyValueFactory<RFDDomain, Number>("status")); 

     TableColumn<RFDDomain, String> column4 = new TableColumn<RFDDomain, String>("Action"); 
     column4.setCellValueFactory(new PropertyValueFactory<RFDDomain, String>("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<ActionEvent>() { 
        @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; 
     } 
    } 
} 
+1

在你的代码中,你将现在空的行设置为纯背景吗? – scottb

+0

button1.setOnAction - 仅设置2行,但3-4行的样式不重新加载。 – stdex

+1

我在您的代码中看到您为包含数据的行设置样式,但我没有看到您设置的行的位置已更改,因此它们不包含纯数据样式的数据。顺便说一句,你为什么要调用recheck_table()这么多?你只需要设置行工厂一次。 – scottb

回答

12

尝试tableView.refresh()。它自Java 8u60起可用于JavaFX。

+0

它不适合我! –

+0

这对我来说也不适用。 JDK 9。 – atlantis

1

这里是一个小把戏我发现在​​一年前...

myTableView.getColumns().get(0).setVisible(false); 
myTableView.getColumns().get(0).setVisible(true); 

这可能不是最好的方式,但它适合我...

+0

是不正确的,如果你在其他行中有变化,该方法返回表完成到加载数据的最后一个状态 – Doberon