调用 PropertyValueFactory 时指定参数

Posted

技术标签:

【中文标题】调用 PropertyValueFactory 时指定参数【英文标题】:Specifying a parameter when calling PropertyValueFactory 【发布时间】:2016-02-19 20:15:39 【问题描述】:

我目前正在 JavaFX 中创建一个 TableView,我想知道在创建 TableColumn 时是否可以传入参数,这是我的代码 sn-p:

// Create column
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy");
TableColumn<WeeklyReport,String> tc = new TableColumn<WeeklyReport,String>(sdf.format(d));
tc.setCellValueFactory(new PropertyValueFactory<>("WC"));

这会调用 tableview 中每个 WeeklyReport 对象的 getWC(),但是我需要一种将日期传递给指定日期的方法。

如果没有,是否有任何解决方法或其他简单的方法可以做到这一点?

【问题讨论】:

【参考方案1】:

PropertyValueFactory 无法做到这一点。直接实现回调即可:

tc.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getWC(...)));

【讨论】:

以上是关于调用 PropertyValueFactory 时指定参数的主要内容,如果未能解决你的问题,请参考以下文章