简体   繁体   English

带有嵌套Bean的JavaFX TableColumn

[英]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_Beanqwerty变量的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM