Programatically selecting cells in a uitable
34 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Brian
el 16 de Jul. de 2017
Respondida: Adam Danz
el 10 de Mayo de 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:
2 comentarios
Jan
el 17 de Jul. de 2017
If you have found some almost useful information, share it with the readers. Otehrwise they might use the search function and must test, if you meant the same thread.
Jan
el 18 de Jul. de 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?
Respuesta aceptada
Brian
el 18 de Jul. de 2017
Editada: Jan
el 20 de Nov. de 2019
4 comentarios
Adam Danz
el 6 de Dic. de 2019
Note that this method does not work in app designer UI tables.
Also note that the findjobj() function uses the JavaFrame property and javacomponent function which will no longer be supported by Matlab in future releases.
More info:
Más respuestas (4)
Can Yuce
el 24 de Ag. de 2018
Editada: Can Yuce
el 24 de Ag. de 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.
0 comentarios
Gene Sanchez
el 17 de Nov. de 2019
Editada: Jan
el 20 de Nov. de 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
jUITable.getSelectedRows
ans =
3×1 int32 column vector
1
2
5
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).
0 comentarios
HAO YUAN
el 19 de Mayo de 2019
jtableobj=findjobj(tableobj); % tableobj, uitable object
SelMod=jtableobj.getTableSelectionModel;
SelMod.clearSelection;
% rows cols of cells
for irows=1:length(rows)
SelMod.addSelection(rows(irows)-1,cols(irows)-1);
end
0 comentarios
Adam Danz
el 10 de Mayo de 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:
- scroll(uit,vlocation) where vlocation is 'top'-'bottom'
- scroll(uit,hlocation) where hlocation is 'left'-'right'
- scroll(uit,target,targetIndex) where target is 'row'-'column'-'cell' and targetIndex is a row or column number or a 1x2 cell location.
0 comentarios
Ver también
Categorías
Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!