I have a TreeTableView
with two columns. The first column shows a label and the second column shows an action text with a custom CellFactory
.
The first time I expand the node, the action text is set correctly. But when I repeat to expand the Node, the action text doesn't update.
The screenshots demonstrate the not expected behavior: application start, first expand, second expand
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.TreeItemPropertyValueFactory;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class MainApp extends Application {
public static void main(String[] args) {
Application.launch(MainApp.class, args);
}
@Override
public void start(Stage primaryStage) throws Exception {
StackPane stackPane = new StackPane();
final TreeItem<LabeldTreeItem> root = buildTree();
// Creating columns
TreeTableColumn<LabeldTreeItem, String> labelColumn = new TreeTableColumn<>("label");
TreeTableColumn<LabeldTreeItem, String> actionColumn = new TreeTableColumn<>("action");
labelColumn.setMinWidth(120);
actionColumn.setMinWidth(120);
// Defining cell content
labelColumn.setCellValueFactory(new TreeItemPropertyValueFactory<>("label"));
// cell factory to display action text **click me!**
actionColumn.setCellFactory(ttc -> new TreeTableCell<LabeldTreeItem, String>() {
@Override
protected void updateItem(String value, boolean empty) {
super.updateItem(value, empty);
final LabeldTreeItem item = this.getTreeTableRow().getItem();
this.setText(null);
this.setGraphic(null);
if (empty) {
this.setText("empty");
} else if (item == null) {
this.setText("null item");
} else if (item.isClickMe()) {
this.setText("click me!");
}
}
});
//Creating a tree table view
final TreeTableView<LabeldTreeItem> treeTableView = new TreeTableView<>(root);
treeTableView.getColumns().addAll(List.of(labelColumn, actionColumn));
treeTableView.setShowRoot(false);
stackPane.getChildren().add(treeTableView);
primaryStage.setScene(new Scene(stackPane, 300, 250));
primaryStage.show();
}
public class LabeldTreeItem {
private final String label;
private final boolean clickMe;
public LabeldTreeItem(String label, boolean clickMe) {
this.label = label;
this.clickMe = clickMe;
}
public String getLabel() {
return label;
}
public boolean isClickMe() {
return clickMe;
}
}
private TreeItem<LabeldTreeItem> buildTree() {
final TreeItem<LabeldTreeItem> root = new TreeItem<>();
root.setExpanded(true);
final TreeItem<LabeldTreeItem> node = new TreeItem<>(new LabeldTreeItem("Node", false));
final TreeItem<LabeldTreeItem> item1 = new TreeItem<>(new LabeldTreeItem("Item 1", true));
final TreeItem<LabeldTreeItem> item2 = new TreeItem<>(new LabeldTreeItem("Item 2", false));
final TreeItem<LabeldTreeItem> item3 = new TreeItem<>(new LabeldTreeItem("Item 3", true));
node.getChildren().addAll(List.of(item1, item2, item3));
root.getChildren().add(node);
return root;
}
}
I have tried these with Java 11 and JavafX 16.