Run the enclosed program and then expand either the A or B node. The children appear without the proper height to display the two labels. Clicking on any node in the tree then fixes the nodes and they display correctly. Expanding or collapsing any node in the tree makes the problem re-appear.
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TreeWrapBug extends Application {
public static void main(String[] args) {
Application.launch(args);
}
public void start(Stage stage) throws Exception {
TreeItem<String> root = new TreeItem<>("ROOT");
TreeItem<String> a = new TreeItem<>("A");
root.getChildren().add(a);
TreeItem<String> b = new TreeItem<>("B");
root.getChildren().add(b);
a.getChildren().add(new TreeItem<>("A.1"));
a.getChildren().add(new TreeItem<>("A.2"));
a.getChildren().add(new TreeItem<>("A.3"));
b.getChildren().add(new TreeItem<>("B.1"));
b.getChildren().add(new TreeItem<>("B.2"));
b.getChildren().add(new TreeItem<>("B.3"));
TreeView<String> tv = new TreeView<>(root);
tv.setCellFactory(stringTreeView -> new TestCell());
tv.setShowRoot(false);
BorderPane bp = new BorderPane(tv);
Scene scene = new Scene(bp, 800, 600);
stage.setScene(scene);
stage.show();
}
private static class TestCell extends TreeCell<String> {
protected void updateItem(String s, boolean empty) {
super.updateItem(s, empty);
if (s == null || empty) {
setText(null);
setGraphic(null);
}
else {
Label l1 = new Label(s);
Label l2 = new Label("Another "+s);
VBox v = new VBox(5, l1, l2);
v.setAlignment(Pos.BASELINE_LEFT);
setGraphic(v);
}
}
}
}
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TreeWrapBug extends Application {
public static void main(String[] args) {
Application.launch(args);
}
public void start(Stage stage) throws Exception {
TreeItem<String> root = new TreeItem<>("ROOT");
TreeItem<String> a = new TreeItem<>("A");
root.getChildren().add(a);
TreeItem<String> b = new TreeItem<>("B");
root.getChildren().add(b);
a.getChildren().add(new TreeItem<>("A.1"));
a.getChildren().add(new TreeItem<>("A.2"));
a.getChildren().add(new TreeItem<>("A.3"));
b.getChildren().add(new TreeItem<>("B.1"));
b.getChildren().add(new TreeItem<>("B.2"));
b.getChildren().add(new TreeItem<>("B.3"));
TreeView<String> tv = new TreeView<>(root);
tv.setCellFactory(stringTreeView -> new TestCell());
tv.setShowRoot(false);
BorderPane bp = new BorderPane(tv);
Scene scene = new Scene(bp, 800, 600);
stage.setScene(scene);
stage.show();
}
private static class TestCell extends TreeCell<String> {
protected void updateItem(String s, boolean empty) {
super.updateItem(s, empty);
if (s == null || empty) {
setText(null);
setGraphic(null);
}
else {
Label l1 = new Label(s);
Label l2 = new Label("Another "+s);
VBox v = new VBox(5, l1, l2);
v.setAlignment(Pos.BASELINE_LEFT);
setGraphic(v);
}
}
}
}