I have a situation where the state of the JavaFX TreeView is not correct when the items are added to the root one by one which can be recreated with the code below.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class App extends Application {
@Override
public void start(Stage stage) {
TreeView<String> treeView = new TreeView<>();
TreeItem<String> root = new TreeItem<>("Root");
treeView.setRoot(root);
treeView.setShowRoot(false);
root.getChildren().add(new TreeItem<>("Foo"));
root.getChildren().add(new TreeItem<>("Bar"));
root.getChildren().add(new TreeItem<>("Baz"));
var scene = new Scene(new StackPane(treeView), 640, 480);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
As you can see the second item has a blue rectangle around it (I think this means the item is focused). Using treeView.refresh() or treeView.getFocusModel() does not seem to clear this up however as soon as I interact with the TreeView using the keyboard and mouse everything is corrected.
If I change the code above to replace the three calls to add with addAll then nothing is selected or focused when the window opens.
I have noticed that if the root node is added to the TreeView after all the items have been added to it then the state of the tree view is consistent when using add or addAll; In these cases the first item in the TreeView is surrounded by a blue rectangle.
Can anyone explain the difference in behaviour? Is this a bug?
This was tested using version 15.0.1 of the JDK and JavaFX.
