lundi 25 novembre 2019

JavaFX: display stored CheckBoxTreeItem selection in TreeView

I use a treeView to select sections which later shall be displayed on a report. To improve the user-friendliness I decided to save the selection, so the user doesn't have to select the same sections the next time again. I have saved the selections, but when the view is initialized, the treeView doesn't display the selection properly (img 1). I'm looking for a way that displays the treeView as it is shown in img 2.

public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        CheckBoxTreeItem<String> rootItem = new CheckBoxTreeItem<>("Root");

        CheckBoxTreeItem<String> aItem = new CheckBoxTreeItem<>("A");

        CheckBoxTreeItem<String> a1Item = new CheckBoxTreeItem<>("A_1");
        CheckBoxTreeItem<String> a2Item = new CheckBoxTreeItem<>("A_2");

        CheckBoxTreeItem<String> bItem = new CheckBoxTreeItem<>("B");
        CheckBoxTreeItem<String> cItem = new CheckBoxTreeItem<>("C");

        a1Item.setSelected(true);
        aItem.setExpanded(true);

        aItem.getChildren().addAll(a1Item, a2Item);

        rootItem.getChildren().addAll(aItem, bItem, cItem);
        rootItem.setExpanded(true);

        TreeView treeView = new TreeView(rootItem);
        treeView.setCellFactory(CheckBoxTreeCell.<String>forTreeView());

        Scene scene = new Scene(treeView, 400, 400);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

how it looks at the start how it should look at the start




Aucun commentaire:

Enregistrer un commentaire