简体   繁体   中英

JavaFX TableColumn with nested Bean

i need to show on a TableView a column that is a nested bean like this:

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. 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 .

@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

I need to tell to the tableColumn that it must fetch the qwerty_B field from the A_Bean . 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:

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());
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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