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

91 ビュー (過去 30 日間)
Eivind Hennestad
Eivind Hennestad 2019 年 6 月 27 日
回答済み: Yuval Cohen 2021 年 4 月 5 日
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 Center および File ExchangeMigrate GUIDE Apps についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by