mardi 22 janvier 2019

set CheckBoxTreeItem to always selected

I'm using a tree view to navigate faster in my printing view. Before the user Is printing a document he can select or deselect columns. Some of the columns are mandatory so what I want to do is that they are always selected, even when the user is trying to deselect them. This is an example of how it looks like. How can I set the selection for an item to true and keep it like that?

public class TreeItemExample extends Application {

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("Hello World!");

    AnchorPane root = new AnchorPane();
    TreeView<String> treeView = new TreeView<>();
    CheckBoxTreeItem<String> rootItem = new CheckBoxTreeItem<>("Example");
    CheckBoxTreeItem<String> mandatoryItem = new CheckBoxTreeItem<>("A");
    CheckBoxTreeItem<String> optionalItem = new CheckBoxTreeItem<>("B");

    mandatoryItem.setSelected(true);
    mandatoryItem.selectedProperty().addListener((observable, oldValue, newValue) -> {
        newValue = true;
        mandatoryItem.setSelected(true);
    });
    rootItem.getChildren().addAll(mandatoryItem, optionalItem);
    treeView.setRoot(rootItem);
    treeView.setCellFactory(CheckBoxTreeCell.<String>forTreeView());

    root.getChildren().add(treeView);

    primaryStage.setScene(new Scene(root, 300, 250));
    primaryStage.show();
}

}




Aucun commentaire:

Enregistrer un commentaire