How to control Uitable scroller position?

14 visualizaciones (últimos 30 días)
Sainath
Sainath el 7 de Feb. de 2012
Respondida: Rueben Mendelsberg el 25 de Feb. de 2020
Hi,
when an uitable is updated, uitable automatically resets to focus on the top row of data.
Is there a way to set the scroll position in the table instead of "jumping" every time the table is updated?
Thankyou for your time...
Regards, Sainath M.

Respuesta aceptada

Can Yuce
Can Yuce el 24 de Ag. de 2018
Editada: Can Yuce el 24 de Ag. de 2018
This workaround solution use again the FEX: findjobj functionality and implicitly sets the selection mode to SINGLE_SELECTION. If the user selects a new cell, then the focus is switched to the view making the selected row now visible again.
set(mtable, 'CellSelectionCallBack', @selectionChangeCallBack);
function selectionChangeCallBack(src, eventdata)
if ~isempty( eventdata.Indices)
where_changed = eventdata.Indices(1);
jscrollpane = javaObjectEDT(findjobj(src));
viewport = javaObjectEDT(jscrollpane.getViewport);
jtable = javaObjectEDT( viewport.getView );
jtable.scrollRowToVisible(where_changed - 1);
end
end
it requires findjobj functionality, and works fine on Matlab R2017a.
  2 comentarios
mz123
mz123 el 4 de En. de 2019
In a code that sounds more or less like this:
N = input('How many columns?\n')
f = figure;
set(gcf, 'units', 'normalized',...
'position', [0.05 0.15 0.7 0.4]) % [left bottom width height]
uit = uitable(f);
uit.Data = cell(3,N); % 3 is just to type a small value to visualize the effect
uit.Units = 'normalized';
uit.Position(3:4) = uit.Extent(3:4);
row = 1;
col = 1;
% Continuos acquisition - here only some part of the code is shown
while 1
d = fscanf(obj1,'%c'); % read from instrument obj1
uit.Data{row,col} = d; % put the value in the cell at specific index
drawnow % update the table
col = col + 1; % increment column index
if col > N % restart from column index 1 and pass on a new row of the table
col = 1;
row = row + 1;
end
end
Now, when the index row is getting 4, 5, 6, 7 .... etc, I suffer the problem mentioned by Sainath, and I'd like to understand how to use the piece of code you suggested.
Thank you.
mz123
mz123 el 7 de En. de 2019
Any suggestion? I was not able to fix the problem.. I defined the function selectionChangeCallBack(src, eventdata) locally at the end of the script, and called the set(mtable, 'CellSelectionCallBack', @selectionChangeCallBack) firstly outside the while loop and then inside the loop, but the problem is still present.
Where am I doing wrong? Thanks.

Iniciar sesión para comentar.

Más respuestas (2)

Jan
Jan el 7 de Feb. de 2012
You can use Yair's FEX: findjobj to get the Java handle of the table. Then there are all possibilities y programmer can need.
Please ask the technical support for this problem also. The more users ask for improvements of the currently very weakly supported UITAB, UITABLE and UITREE, the more like is an update.
  2 comentarios
Matthew Worker
Matthew Worker el 25 de Abr. de 2018
Hi yes, I have this issue and unable to solve it? In Matlab 2018a has there been a solution?
Walter Roberson
Walter Roberson el 4 de En. de 2019
there was no change in r2018a or r2018b

Iniciar sesión para comentar.


Rueben Mendelsberg
Rueben Mendelsberg el 25 de Feb. de 2020
The answer provided by Can is pretty close but doesn't bring the table back to exactly where the focus was before you started. It just makes whatever row you interacted with one up from the bottom.
Also, there seems to be the need to throw a drawnow() command before resetting the uitable scroll position.
To get back to the exact view you started from you need to insert at the beginning of the SelectionChangeCallback or the CellEditCallback:
function callbackFcn(src,evt)
jscrollpane = javaObjectEDT(findjobj(src));
viewport = javaObjectEDT(jscrollpane.getViewport);
P = viewport.getViewPosition();
jtable = = javaObjectEDT( viewport.getView );
% Do whatever you need to do in the callback...
%
%
drawnow() %This is necessary to ensure the view position is set after matlab hijacks it
viewport.setViewPosition(P);
end
The table will flicker as matlab forces it back to the start and java pulls it back to where it was, but that is the cleanest implementation that is made more efficient if the gui is written as a handle class.

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by