How to implement right-click and double click in matlab uitables?

91 次查看(过去 30 天)
I am making a gui with a table of elements, that I want to access and control using the mouse. For example, I want to be able to highlight/select items, double click on items and right click items, much like how I can interact with files and folders in finder or windows explorer.
The CellSelectionCallback is great, because it gives access to which cell is selected. However, it seems I cannot implement a doubleclick action since the CellSelectionCallback does not trigger if a cell is already selected, and the ButtonDownFcn does not capture left mouseclicks any more.
I can also implement a uipopupmenu, but whenever I rightclick a cell, 1) that cell does not get highlighted (which I find very non-intuitive) and 2) I dont have access to which cell was selected (unless I get the mouse cursor position, scrollbar position etc. to figure out which cell was clicked upon).
Am I missing an obvious way to implement what I want? Is there any reason why these kinds of mouse operations (capturing double clicks and right clicks) on specific cells, which are so common place in both file browsers and spreadsheet software are not easy to implement in uitables?
  1 个评论
Sven
Sven 2019-11-11
Upvoted for visibility. Here's some minimal working example code.
function uitableCellSelectionExample()
f = uifigure;
myTab = table([1;2],["Click";"or DblClick"]);
uitable(f,'Data',myTab,'CellSelectionCallback',@onCellSelect)
end
function onCellSelect(srcTab,event)
uifigH = srcTab.Parent;
uifigH.SelectionType % This is *always* "normal" even on double-click
event
rand(1) % Because re-clicking a selected cell doesn't trigger a new event
end

请先登录,再进行评论。

采纳的回答

Yuval Cohen
Yuval Cohen 2021-4-5
The issue is that the CellSelectionCallback is triggered on the first, and only the first click and the callback function will not yet know if it is a single or double click.
The simple workaround I found is to add the following code to the callback function:
function uitableCellSelectionExample()
fig_handle = uifigure;
data = num2cell(magic(5));
uitable(fig_handle,'Data',data,'CellSelectionCallback',{@cell_click, fig_handle}) % pass the figure handle as an argument
end
% ----------------------------------------------------------------------
function cell_click(table_handle, event, fig_handle)
pause(0.5); % to allow the user time to add a second click
if strcmpi(fig_handle.SelectionType, 'open')
fprintf(1, 'Double click on cell %d, %d\n', event.Indices(1,1), event.Indices(1,2));
else
fprintf(1, 'Single click on cell %d, %d\n', event.Indices(1,1), event.Indices(1,2));
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by