Cell-specific context menu in uitable

조회 수: 39 (최근 30일)
Justin
Justin 2021년 11월 15일
댓글: Justin 2024년 3월 1일
We're replacing the Java components in our GUIs because they won't be supported soon. One feature that I've not been able to replicate adequately with native Matlab is cell-specific right-click callbacks in a table. Hopefully someone can help.
Initial plan was uitable in a figure, but the problem is that the right-click related events don't have the Indices property. I can store the Indices on a left-click induced TableCellSelect and pick them up on right-click with a TableButtonDown function. I can also hide the context menu if no cell is selected. But what I can't seem to handle is the case where one cell is selected but the user right clicks on another. The stored Indices are out of date and there doesn't seem to be any way to get the new values? Any thoughts appreciated.
I appreciate that uitable has more functionality within a uifigure but that is not an option here. We're not switching most our GUIs to uifigure until the performance issues we found and others have reported on Matlab Central are fixed.
Function to illustrate problem, I want up-to-date Indices info in the ContextMenuOpeningFcn if user right clicks different cell to the the one selected. Or some other way to do cell-specific context menus.
function table_tester
hf = figure('WindowButtonDownFcn',@CallbackFigure);
ht = uitable(hf,'Data',rand(5),'Enable','on','ButtonDownFcn',@TableButtonDown,'CellSelectionCallback',@TableCellSelect);
cm = uicontextmenu(hf,'ContextMenuOpeningFcn',@ContextMenuOpeningFcn);
set(ht,'ContextMenu',cm);
function CallbackFigure(hf,evt,~)
disp('CallbackFigure called')
disp(evt.EventName)
disp(hf.SelectionType)
switch hf.SelectionType
case 'alt'
disp('Right click Figure')
case 'normal'
disp('Left click Figure')
end
end
function TableButtonDown(ht,evt,~)
disp('TableButtonDown called')
disp(evt.EventName)
disp(hf.SelectionType)
switch hf.SelectionType
case 'alt'
disp('Right click Table')
Indices = getappdata(ht,'Indices');
disp(Indices)
case 'normal'
disp('Left click Table')
end
disp(' ')
end
function TableCellSelect(ht,evt,~)
pause(0.1)
disp('TableCellSelect called')
disp(evt.EventName)
disp(hf.SelectionType)
switch hf.SelectionType
case 'alt'
% Never happens
disp('Right click Table')
case 'normal'
setappdata(ht,'Indices',evt.Indices)
disp('Left click Table; indices stored to table')
end
end
function ContextMenuOpeningFcn(cm,evt,~)
pause(0.1)
disp('ContextMenuOpeningFcn called')
disp(evt.EventName)
disp(hf.SelectionType)
Indices = getappdata(ht,'Indices');
disp(Indices)
delete(cm.Children)
if isempty(Indices)
disp('Context menu not created')
return
else
disp('Context menu active')
if Indices(2) < 3
uimenu(cm,'Text','Exclude Point','MenuSelectedFcn',@ContextMenuItemCallback);
else
uimenu(cm,'Text','Include Point','MenuSelectedFcn',@ContextMenuItemCallback);
end
end
end
function ContextMenuItemCallback(hm,evt,~)
pause(0.1)
disp('ContextMenuItemCallback called')
disp(evt.EventName)
disp(hf.SelectionType)
Indices = getappdata(ht,'Indices');
disp(Indices)
end
end

채택된 답변

Divyanshu
Divyanshu 2024년 2월 27일
편집: Divyanshu 2024년 2월 27일
Hi Justin,
A possible way to achieve location specific context-menu opening through App designer is by adding the following piece of code inside the function 'ContextMenuOpeningFcn' you have defined:
row = event.InteractionInformation.Row;
app.currentRow = row;
% The above piece of code enables you to track the row of a UITable which user
% has clicked or from which the context-menu is triggered.
% However, it will not give information about the cell from which it is
% triggered.
I have used this code for version R2023b, there it works well but not sure if it will work for earlier versions of MATLAB.
  댓글 수: 1
Justin
Justin 2024년 3월 1일
Hi Divyanshu,
I checked and this does indeed work in 2023b.
Indices = [evt.InteractionInformation.Row evt.InteractionInformation.Column];
Must be a new feature as it doesn't work in 2020b which is what I was using when I posted the question.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by