Reputation: 423
I have spent several hours investigating a problem and just can't get my head around it. I have found this link and that link already, but even though I think I followed the suggestions there, my TreeTableView does not get updated.
I have a controller in which I use the initialize-Method to set the cell- and cellValue-Factories:
aktivaValueTreeTableColumn.setCellValueFactory(new TreeItemPropertyValueFactory<>("valueAsString"));
aktivaValueTreeTableColumn.setCellFactory(Helper.getPaddingCallbackFactory());
aktivaValueTreeTableColumn.setStyle(StyleHelper.alignTextRight());
aktivaActionColumn.setStyle(StyleHelper.alignTextRight());
aktivaActionColumn.setCellValueFactory(new Callback<TreeTableColumn.CellDataFeatures<BalanceSheetItem, String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(TreeTableColumn.CellDataFeatures<BalanceSheetItem, String> param) {
if (param == null || param.getValue() == null || param.getValue().getValue() == null || param.getValue().getValue().getAccount() == null) {
return new SimpleStringProperty(StringUtils.EMPTY);
}
return param.getValue().getValue().getAccount().getAccountNumberSimpleString();
}
});
// create a cell value factory with an add button for each row in the table.
aktivaActionColumn.setCellFactory(
new Callback<TreeTableColumn<BalanceSheetItem, String>, TreeTableCell<BalanceSheetItem, String>>() {
@Override
public TreeTableCell<BalanceSheetItem, String> call(
TreeTableColumn<BalanceSheetItem, String> param) {
return new ShowFinancialBookingsCell();
}
});
refreshTreeTableViews();
In the refreshTreeTableViews() I am adding several Items to the TreeTableView. For the TreeItemPropertyValueFactories this workes fine, everything get's rendered correctly as the TreeTableView shows up. However, the cells where the ShowFinancialBookingsCell() is, don't get rendered initially. When I scroll down and up again, the part of the TreeTableView which was hidden and came into appearance again correctly shows the ShowFinancialBookingsCell()'s.
Due to this I tried to refresh the TreeTableView after the refreshTreeTableViews()-Function, but this did not change anything.
The code of the ShowFinancialBookingsCell is:
public class ShowFinancialBookingsCell extends TreeTableCell<BalanceSheetItem, String> {
@Override
protected void updateItem(String accountNo, boolean empty) {
super.updateItem(accountNo, empty); //To change body of generated methods, choose Tools | Templates.
if (isEmpty()) {
setGraphic(null);
setText(null);
} else if (accountNo != null && StringUtils.isNotBlank(accountNo)) {
refreshView(accountNo);
} else {
// If this is the root we just display the text.
setGraphic(null);
setText(null);
}
}
private void refreshView(String account) {
Button button = new Button();
HBox hbox = new HBox();
button.setPrefWidth(40);
hbox.setPadding(new Insets(0, 0, 0, 10));
button.setGraphic(new ImageView(ImageProvider.getImageForName(ImageProvider.OPEN_SYMBOL)));
button.setOnAction(new EventHandler<javafx.event.ActionEvent>() {
@Override
public void handle(javafx.event.ActionEvent event) {
List<Account> a = AccountManagerImpl.getInstance().findByAccountNumber(account);
if (CollectionUtils.isNotEmpty(a)) {
try {
WindowHelper.showObjectListDisplay(new ArrayList<>(FinanceManagerImpl.getInstance().findAllForAccount(a.get(0))));
} catch (IOException ex) {
Logger.getLogger(ShowFinancialBookingsCell.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
hbox.getChildren().add(button);
this.getChildren().clear();
this.getChildren().add(hbox);
}
}
Has anybody got an idea what I am doing wrong in my custom cells? The other cells (having a TreeItemPropertyValueFactory) render nicely.
Upvotes: 0
Views: 361
Reputation: 209309
Instead of adding children to the cell, you should set the graphic:
hbox.getChildren().add(button);
// this.getChildren().clear();
// this.getChildren().add(hbox);
this.setGraphic(hbox);
The cell likely manages its own layout and probably builds its child node list whenever it performs layout, in order to display the text and graphic.
Upvotes: 1