enable context menu for specific cell or item in uitable or uilistbox in matlab

조회 수: 15 (최근 30일)
This is a cross posting from:
Had no luck there so far.
Basically, I'd like to know how to get a context menu to fire on a specific item in a uitable or uilistbox. There seems to be no way to do this without first click on an entry, and then right clicking at the exact same location and assuming that the user hasn't moved their mouse. Even this double clicking approach (left, then right) would be super awkward.

채택된 답변

Jim Hokanson
Jim Hokanson 2022년 3월 25일
편집: Jim Hokanson 2024년 12월 3일 2:10
Edit: For R2023b and newer the solution by Jasmine (see answer below) is the way to go
----------------------------------------------------------------
Here's a solution with left click. Couldn't get right to work. More info at linked StackOverflow page above. Code copied here:
classdef wtf < handle
properties
h %struct, this was an appdesigner handle
cm %context menu
end
methods
function obj = wtf()
h = struct;
h.UIFigure = uifigure();
h.UITable = uitable(h.UIFigure);
obj.h = h;
obj.h.UITable.SelectionChangedFcn = @obj.tableCall;
%Some data ...
s = struct;
s.a = (1:4)';
s.b = (5:8)';
obj.h.UITable.Data = struct2table(s);
%Our context menu
cm = uicontextmenu(obj.h.UIFigure);
m = uimenu(cm,'Text','Menu1');
obj.cm = cm;
%WTF ... without this we don't get a valid CurrentPoint
obj.h.UIFigure.WindowButtonMotionFcn = @obj.mouseMove;
end
function tableCall(obj,x,y)
cp = get (obj.h.UIFigure, 'CurrentPoint');
open(obj.cm,cp(1),cp(2));
selected_cell = y.Selection;
%Now we can run something on the context menu
%that targets the selected cell
end
function mouseMove(obj,x,y)
%we could store a point here
end
end
end

추가 답변 (1개)

Jasmine Poppick
Jasmine Poppick 2023년 9월 20일
Starting in R2023b, you can use the InteractionInformation property of the ContextMenuOpeningFcn callback event data to query the table cell or listbox item that was right-clicked to open the context menu, and then show or hide the context menu based on whether you want the menu items to appear.
Here is an example in the documentation that shows how to selectively show a context menu on certain parts of a table.

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by