0
votes

I have a JavaFX TreeView. Some of the nodes can be unselectable. An unselectable node can have child nodes that are selectable.

I can disable the TreeCell with .setDisable, then the node is correctly unselectable (by mouse). However setting the cell as disable also disables the expand/collapse arrow. Also weirdly I can still select and expand/collapse via keyboard.

Is is possible to make certain nodes unselectable but still allow expanding them?

1
Being able to interact with a disabled tree cell via the keyboard sounds like a bug. The best way to modify the selection semantics is probably to implement your own selection model, which is quite a pain but is possible. See stackoverflow.com/questions/33655663/… for a similar solution.James_D
Ok, I try that (or try to implement some other control etc if it seems to be too hard)Bjarne Boström

1 Answers

-1
votes

There is a walk around for this using CSS:

After you define your tree and nodes - set a root on your tree view

TreeItem<String> root = new TreeItem<String>("NAME_OF_YOUR_ROOT");
treeView.setRoot(root); 

Add a listener to your tree view

treeView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> 
handleTreeCellSelction(newValue));

handleTreeCellSelction method will ensure that

private void handleTreeCellSelction(TreeItem<String> newValue) 
{
    String nameOfTreeItem = newValue.getValue();
    if(nameOfTreeItem.equals("NAME_OF_YOUR_ROOT"))
    {
          newValue.setStyle("/*ALL the css styles to make it look its not selected*/");
    }
}