[英]JavaFX TableColumn with nested Bean
i need to show on a TableView
a column that is a nested bean like this: 我需要在
TableView
上显示像这样的嵌套bean的列:
public class A_Bean {
private ObjectProperty<BigDecimal> id;
private B_Bean qwerty;
}
public class B_Bean {
private ObjectProperty<BigDecimal> id;
private StringProperty qwerty_B;
}
All the bean class has the get, set and property method. 所有的bean类都有get,set和property方法。 My problem is that
A_Bean
has a variable of B_Bean
type, and i don't know how to tell to the tableColumn
that it must display the qwerty_B
field of B_Bean
and not the pointer to the qwerty
variable of A_Bean
. 我的问题是
A_Bean
具有B_Bean
类型的变量,我不知道如何告诉tableColumn
它必须显示qwerty_B
字段,而不是B_Bean
的qwerty
变量的A_Bean
。
@FXML
private TableView<A_Bean> myTable;
@FXML
private TableColumn<A_Bean, BigDecimal> idColumn;
@FXML
private TableColumn<A_Bean, B_Bean> qwertyColumn;
.....
@FXML
private void initialize() {
idColumn.setCellValueFactory(cellData -> cellData.getValue().idProperty() );
qwertyColumn.setCellValueFactory(cellData -> cellData.getValue().qwertyProperty() );
.....
}
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
myTable.setItems(mainApp.getA_BeanData());
}
If if run my code i'll see on the tableColumn
something like xx.B_Bean@1234dasf23
如果运行我的代码,我会在
tableColumn
上看到类似xx.B_Bean@1234dasf23
I need to tell to the tableColumn
that it must fetch the qwerty_B
field from the A_Bean
. 我需要告诉
tableColumn
它必须从A_Bean
获取qwerty_B
字段。 How can i do that? 我怎样才能做到这一点?
Just use a cell factory to tell the cell how to display the B_Bean
it contains as its value: 只需使用单元格工厂告诉单元格如何显示其包含的
B_Bean
作为其值即可:
qwertyColumn.setCellFactory(tc -> new TableCell<A_Bean, B_Bean>() {
@Override
protected void updateItem(B_Bean item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? null : item.getQwerty_B());
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.