How to set the interactivity of uitreenode to 'off'

13 views (last 30 days)
Question is in the title,
I have a uitree, with top level nodes and children nodes.
I want to disable the interactivity with the children nodes.
When the top level node is checked/unchecked, the children are checked/uncheckd. But the use cannot uncheck/check the children node .

Answers (2)

Hitesh
Hitesh on 11 Jun 2025
Hi Sylvain,
"uitreenode" objects don't have a direct 'Enable' or 'Interactivity' property to disable user interaction with individual nodes like UI controls do. However, you will be able to simulate non-interactivity by using the Checked property and controlling behavior via a callback, essentially preventing user-initiated changes on the child nodes. Kindly follow the below approach to set the interactivity of uitreenode to 'off'.
  1. Use the CheckedNodesChangedFcn of the uitree.
  2. Revert any user interaction with the child nodes.
  3. Allow interaction only through the top-level node.
Refer to following code as an example.
fig = uifigure;
t = uitree(fig, 'checkbox', 'Position', [20 20 200 200]);
% Create top-level node
topNode = uitreenode(t, 'Text', 'Parent Node');
% Create child nodes
child1 = uitreenode(topNode, 'Text', 'Child 1');
child2 = uitreenode(topNode, 'Text', 'Child 2');
% Expand top-level node to show children
expand(topNode);
% Store list of read-only nodes (children)
readOnlyNodes = [child1, child2];
% Set callback to control interaction
t.CheckedNodesChangedFcn = @(src, event) treeCallback(src, event, topNode, readOnlyNodes);
function treeCallback(src, event, topNode, readOnlyNodes)
newChecked = src.CheckedNodes;
if ismember(topNode, newChecked)
% Top node is checked — enforce all children to be checked
src.CheckedNodes = vertcat(topNode, readOnlyNodes(:));
else
% Top node is unchecked — uncheck all
src.CheckedNodes = [];
end
end
For more information regarding "uitreenode", kindly refer to the following MATLAB documentation:

Sylvain
Sylvain on 12 Jun 2025
Thank you, shame that such interactivity is not implemented. Your code seems to work OK, but graphically it has some visual glitch which affect the rendering,
I have reconsidered my approach by not using he checked node function, but going for the selected nodes instead. For whoever want to have a try, here is the code:
close all force
clear
clc
buildTreeNode()
function buildTreeNode()
fig = uifigure;
tree = uitree(fig, 'Position', [20 20 200 400],'Multiselect','on');
% Create top-level node
topNode(1) = uitreenode(tree, 'Text', 'Parent Node 1');
% Create child nodes
uitreenode(topNode(1), 'Text', 'Child 1 1');
uitreenode(topNode(1), 'Text', 'Child 1 2');
topNode(2) = uitreenode(tree, 'Text', 'Parent Node 2');
% Create child nodes
c(1) = uitreenode(topNode(2), 'Text', 'Child 2 1');
c(2) = uitreenode(topNode(2), 'Text', 'Child 2 2');
uitreenode(topNode(2), 'Text', 'Child 2 3');
uitreenode(topNode(2), 'Text', 'Child 2 4');
cc(1) = uitreenode(c(1), 'Text', 'Child 2 1 1');
uitreenode(c(1), 'Text', 'Child 2 1 2');
uitreenode(c(1), 'Text', 'Child 2 1 3');
uitreenode(c(2), 'Text', 'Child 2 2 1');
uitreenode(cc(1), 'Text', 'Child 2 1 1 1');
topNode(3) = uitreenode(tree, 'Text', 'Parent Node 3');
% Create child nodes
uitreenode(topNode(3), 'Text', 'Child 3 1');
uitreenode(topNode(3), 'Text', 'Child 3 2');
uitreenode(topNode(3), 'Text', 'Child 3 3');
% Expand top-level node to show children
for i=1:length(topNode)
expand(topNode(i),'all');
end
% Select the nodes
j = 1;
tree.SelectedNodes = [tree.Children(j),getAllChildrenCell(tree.Children(j))'];
tree.SelectionChangedFcn = @(src, event) treeCallback(src, event);
end
function treeCallback(src, ~)
selectedNode = src.SelectedNodes;
src.Multiselect = 'off';
topnode = findTopNode(src,selectedNode);
src.Multiselect = 'on';
src.SelectedNodes = [topnode,getAllChildrenCell(topnode)'];
end
function node = findTopNode(tree,node)
if length(node)>1
node = node(1);
end
if ismember(node.Parent,tree)
disp("top node selected")
else
disp("child node selected, looking for parent node")
node = findTopNode(tree,node.Parent);
end
end
function allChildren = getAllChildrenCell(node)
allChildren = {};
function collectChildren(currentNode)
children = currentNode.Children;
if ~isempty(children)
allChildren{end+1} = children;
for i = 1:length(children)
collectChildren(children(i));
end
end
end
collectChildren(node);
% Flatten the cell array
allChildren = vertcat(allChildren{:});
end

Tags

Products


Release

R2025a

Community Treasure Hunt

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

Start Hunting!