Is there any way to find out which specific instance of a UIContextMenu is being called?

4 visualizaciones (últimos 30 días)
So here's my basic setup:
A UIFigure with a UITree, containing multiple UITreeNodes. I want to be able to right click on one of the UITreeNodes, which will do an operation specifically on that node. Let's say I just want to display the text present in the node from which I right clicked.
For example, consider this setup:
figHandle = uifigure;
ctxMenu = uicontextmenu(figHandle);
menuOpt = uimenu(ctxMenu,'text','printNodeText','Callback',@doSomethingCallback);
baseTree = uitree(figHandle);
node1 = uitreenode(baseTree,'text','node1text','ContextMenu',ctxMenu);
node2 = uitreenode(baseTree,'text','node2text','ContextMenu',ctxMenu);
function doSomethingCallback(src,ev)
%for example, display right-clicked UITReeNode's text, either "node1text" or "node2text"
end
When I right click node1 and click printNodeText from there, I want "node1text" displayed. When I right click node2 and click printNodeText from there, I want "node2text" diplayed.
Solutions that I'm pretty sure don't work:
Get the current object - passing figHandle to the callback and then accessing figHandle.CurrentObject will get me just to baseTree, not to node1.
get the current object then selected uitreenode - So close! Except it doesn't work because the node I right clicked isn't necessarily the same as the current seelcted node, given a right click doesn't change the selected node. If I just opened my figure and right clicked on node1,
baseTree = figHandle.CurrentObject;
selectedNodes = baseTree.SelectedNodes;
disp(selectedNodes.Text);
selectedNodes would be empty, meaning that disp() command would fail, rather than displaying "node1text"
Utilizing ContextMenuOpeningFcn - Unfortunately this doesn't seem to have any path to the node I selected, again just to the figure and UITree
Creating individual context menus - not that this doesn't work per say, but it clutters like nobody's business, given how many nodes I have. The top level figure would end up insanely cluttered with all those contextMenus anchored to it. I'd love to avoid this.

Respuestas (1)

Samay Sagar
Samay Sagar el 22 de Feb. de 2024
Starting from MATLAB R2021b, you can access the node for which the context menu is opened using the “CurrentObject” property. Here’s a simple example of how you can achieve this:
figHandle = uifigure;
ctxMenu = uicontextmenu(figHandle);
menuOpt = uimenu(ctxMenu, 'Text', 'Print Node Text', 'MenuSelectedFcn', @doSomethingCallback);
baseTree = uitree(figHandle, 'Position', [20 20 150 150]);
node1 = uitreenode(baseTree, 'Text', 'node1text');
node1.ContextMenu = ctxMenu;
node1.NodeData = node1;
node2 = uitreenode(baseTree, 'Text', 'node2text');
node2.ContextMenu = ctxMenu;
node2.NodeData = node2;
function doSomethingCallback(src, ~)
ctxMenuHandle = src.Parent;
figHandle = ctxMenuHandle.Parent;
treeNode = figHandle.CurrentObject;
disp(treeNode.Text);
end

Categorías

Más información sobre Develop uifigure-Based Apps en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by