Is it possible to disable parent node selection in a uitree?

Is there any way to disable certain nodes in a uitree from being selectable? In particular I have a situation similar to that shown below, where I have multiple parent nodes to group objects within the tree. I want to allow the user to select objects from different groups (e.g. Banana and Carrot here), but I don't want to allow the parent node 'Fruit' to be selected because this makes no sense in my use case, it is just there as a grouping (and I don't wish it to represent selecting every item in that group either, it's not a helpful use case for me).
I'm still relatively new to the App Designer style of UI components, but I don't see any option on the node properties to allow this.

4 Comments

I realise I can use the SelectionChangedFcn as a hack to probably achieve this behaviour (though I haven't yet tried), but that is an ugly solution, requiring me to either store more properties than I otherwise need on my class or traverse the tree every time a selection is made to check if it is a parent node. I was hoping there would be a nice option at node creation time to just disable the ability to select it.
I don't understand why 'Veg' wasn't selected there as well.
You can use NodeData property of treenode component to identify the selection you need. Of course, you have to set it up the way it would make sense for you to identify them while you are creating treenodes.
Just an idea, however, there should be better ways of doing it: If you have two levels, you can use two component vector with first number indicating the parent node, and second number indicating the child node of the first number.
You can use this information in SelectionChangedFcn callback to identify the nodes that do not need to be selected and remove them from SelectedNodes property.
Hope it's clear enough.
You can also use the Tree component with the checkmark, it is in the component browser, it's often overlooked. So you can use the CheckedNodes property to do what you need to do with the selection.
I did look at the Checked tree too, but it also doesn't distinguish at all between a parent node and a leaf node, or allow me to do so.
'Veg' wasn't selected above because I made the selection myself, by clicking on the elements of the tree in multiselect mode and I only clicked 'Fruit' to demonstrate it as an example of what I don't want to be possible.

Sign in to comment.

Answers (1)

Hey,
I hope you have sorted it out, however, here's an example in which SelectionChangedFcn is used to remove the nodes which have children from selection.
h = uitree("Multiselect","on");
fruitH = uitreenode(h, "Text", "Fruit");
vegH = uitreenode(h, "Text", "Veg");
uitreenode(fruitH, "Text", "Apple");
uitreenode(fruitH, "Text", "Fig");
uitreenode(vegH, "Text", "Carrot");
uitreenode(vegH, "Text", "Cabbage");
h.SelectionChangedFcn = @(src, evt) treeSelectionChangedFcn(src,evt);
Function is below
function treeSelectionChangedFcn(src, evt)
selection = src.SelectedNodes;
selectionChildren = {selection(:).Children};
selectionIdx = cellfun(@isempty, selectionChildren);
src.SelectedNodes(~selectionIdx) = [];
end

Products

Release

R2023a

Asked:

on 4 Aug 2023

Answered:

on 14 Aug 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!