Programatically selecting cells in a uitable

86 views (last 30 days)
Brian on 16 Jul 2017
Answered: Adam Danz on 10 May 2021
How would I programatically select a cell, e.g., highlight row 3, column 2 in a uitable?
I found some 5+ year old answers using an obsolete version of uitable that no longer seem to work.
Edit: here is what I found that did not work:
Jan on 18 Jul 2017
There is a bunch of code in this long thread. Please explain, what you have tried and what "did not work" means. Perhaps you had a typo only?

Sign in to comment.

Accepted Answer

Brian on 18 Jul 2017
Edited: Jan on 20 Nov 2019
I figured out the answer by further reviewing the long thread in my question. Here is some updated code that works at least until Matlab R2015b.
% m = numeric handle to uitable
m = uitable(...);
jUIScrollPane = findjobj(m);
jUITable = jUIScrollPane.getViewport.getView;
jUITable.changeSelection(row-1,col-1, false, false);
% subtract 1 from row and col you want selected
% the last two arguments do the following:
% false, false: Clear the previous selection and ensure the
% new cell is selected.
% false, true: Extend the previous selection (select a range of cells).
% true, false: Toggle selection
% true, true: Apply the selection state of the anchor to all cells
% between it and the specified cell.
Using combinations of the last two arguments, you can select any cells you want programatically, even if they're not contiguous.

Sign in to comment.

More Answers (4)

Can Yuce
Can Yuce on 24 Aug 2018
Edited: Can Yuce on 24 Aug 2018
Other solutions exists but may risk race-conditions with the Event Dispatch Thread (EDT). If you are interested in selecting a single row, one working solution running safely on Java Swing’s Event Dispatch Thread (EDT) without risking a race-condition or deadlock with the EDT would be:
jscrollpane = javaObjectEDT(findjobj(src));
viewport = javaObjectEDT(jscrollpane.getViewport);
jtable = javaObjectEDT( viewport.getView );
jtable.scrollRowToVisible(row_ - 1);
it requires findjobj functionality, and works fine on Matlab R2017a.

Gene Sanchez
Gene Sanchez on 17 Nov 2019
Edited: Jan on 20 Nov 2019
I wanted to select multiple cell, but was unable to find an answer on how to do it. The key is on the last two arguments of the function
jUITable.changeSelection(row-1,col-1, false, false);
According to Yair, they are (rowIndex, columnIndex, toggleFlag, extendFlag)
So the following example will select three separate cells:
jUITable.changeSelection(1,1, false, false);
jUITable.changeSelection(1,2, true, false);
jUITable.changeSelection(2,2, true, false);
While this example will select a region with four cells, then another region with five cells:
jUITable.changeSelection(1,1, false, false);
jUITable.changeSelection(1,2, false, true); % extend selection
jUITable.changeSelection(5,1, true, false); % toggle to 2nd selection
jUITable.changeSelection(5,1, true, true); % extend 2nd selection
On a separate note, Finding which cell are selected is not sufficiently done by getSelectedRows
ans =
3×1 int32 column vector
Returns 3 numbers, instead of the five I need to know all the cells that have been selected. Is there an analogous getSelectedCells??
Right now I have to scan the whole set of getSelectedRows and getSelectedColumns to find the currently selected cells, using jUITable.isCellSelected(i,j).

HAO YUAN on 19 May 2019
jtableobj=findjobj(tableobj); % tableobj, uitable object
% rows cols of cells
for irows=1:length(rows)

Adam Danz
Adam Danz on 10 May 2021
Starting in Matlab R2021a, you can programmatically scroll to any row, column, or cell of a uitable using the scroll function (see Four New App Features in MATLAB R2021a).
Syntax examples:



Community Treasure Hunt

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

Start Hunting!