TableColumn JavaFX 的 SetPropertyValueFactory
SetPropertyValueFactory of a TableColumn JavaFX
假设我有一个叫 Employee 的 class。
Employee 有一个 String name、int age 和一个自定义对象 Workstyle ethic。 Workstyle 具有私有字符串样式。
在我们的 GUI class 中,我们创建了 TableColumns:
TableColumn<Professional, String> nameColumn = new TableColumn<>("NAME");
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
//
TableColumn<Employee, Integer> ageColumn = new TableColumn<>("AGE");
nameColumn.setCellValueFactory(new PropertyValueFactory<>("age"));
//
现在我迷路了。我做不到:
TableColumn<Employee, String> workstyleColumn = new TableColumn<>("WORKSTYLE");
workstyleColumn.setCellValueFactory(new PropertyValueFactory<>("ethic.getStyle()"));
必须有一些方法:
TableColumn<Object Being Looked At, Type being put onto the column> columnTitle = new TableColumn<>(
{
// I want to put tons of code here which EVENTUALLY ends with a object of type "Type being put onto the column".
});
感谢您的帮助!
你需要
workstyleColumn.setCellValueFactory(cellData ->
new ReadOnlyStringWrapper(cellData.getValue().getEthic().getStyle()));
cellValueFactory
is a Callback<TableColumn.CellDataFeatures, ObservableValue>
, i.e. a function taking a TableColumn.CellDataFeatures
and returning an ObservableValue
. CellDataFeatures.getValue()
给出了行的值,所以 cellData.getValue().getEthic().getStyle()
给出了你想要的值。最后你将它包裹在 ReadOnlyStringWrapper
中以得到 ObservableValue
.
如果您使用 PropertyValueFactory,您需要编写不带“()”的方法名称。
因此您需要在 Employee 中使用一种方法,该方法具有 returns 道德风格:
class Employee{
private Ethic ethic;
....
public String getEthicStyle(){
return ethic != null ? ethic.getStyle : "";
}
}
其余的看起来像这样:
TableColumn<Employee, String> workstyleColumn = new TableColumn<>("WORKSTYLE");
workstyleColumn.setCellValueFactory(new PropertyValueFactory<>("ethicStyle"));
编辑
@James_D 答案没有错,但如果你真的想使用 PropertyValueFactory,你必须在 Employee
中编写额外的方法
假设我有一个叫 Employee 的 class。
Employee 有一个 String name、int age 和一个自定义对象 Workstyle ethic。 Workstyle 具有私有字符串样式。
在我们的 GUI class 中,我们创建了 TableColumns:
TableColumn<Professional, String> nameColumn = new TableColumn<>("NAME");
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
//
TableColumn<Employee, Integer> ageColumn = new TableColumn<>("AGE");
nameColumn.setCellValueFactory(new PropertyValueFactory<>("age"));
//
现在我迷路了。我做不到:
TableColumn<Employee, String> workstyleColumn = new TableColumn<>("WORKSTYLE");
workstyleColumn.setCellValueFactory(new PropertyValueFactory<>("ethic.getStyle()"));
必须有一些方法:
TableColumn<Object Being Looked At, Type being put onto the column> columnTitle = new TableColumn<>(
{
// I want to put tons of code here which EVENTUALLY ends with a object of type "Type being put onto the column".
});
感谢您的帮助!
你需要
workstyleColumn.setCellValueFactory(cellData ->
new ReadOnlyStringWrapper(cellData.getValue().getEthic().getStyle()));
cellValueFactory
is a Callback<TableColumn.CellDataFeatures, ObservableValue>
, i.e. a function taking a TableColumn.CellDataFeatures
and returning an ObservableValue
. CellDataFeatures.getValue()
给出了行的值,所以 cellData.getValue().getEthic().getStyle()
给出了你想要的值。最后你将它包裹在 ReadOnlyStringWrapper
中以得到 ObservableValue
.
如果您使用 PropertyValueFactory,您需要编写不带“()”的方法名称。
因此您需要在 Employee 中使用一种方法,该方法具有 returns 道德风格:
class Employee{
private Ethic ethic;
....
public String getEthicStyle(){
return ethic != null ? ethic.getStyle : "";
}
}
其余的看起来像这样:
TableColumn<Employee, String> workstyleColumn = new TableColumn<>("WORKSTYLE");
workstyleColumn.setCellValueFactory(new PropertyValueFactory<>("ethicStyle"));
编辑
@James_D 答案没有错,但如果你真的想使用 PropertyValueFactory,你必须在 Employee
中编写额外的方法