Azzera filtri
Azzera filtri

Cell-specific context menu in uitable

23 visualizzazioni (ultimi 30 giorni)
Justin
Justin il 15 Nov 2021
Commentato: Justin il 1 Mar 2024
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

Risposta accettata

Divyanshu
Divyanshu il 27 Feb 2024
Modificato: Divyanshu il 27 Feb 2024
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 Commento
Justin
Justin il 1 Mar 2024
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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Interactive Control and Callbacks in Help Center e File Exchange

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by