How to control Uitable scroller position?

8 次查看(过去 30 天)
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.

采纳的回答

Can Yuce
Can Yuce 2018-8-24
编辑:Can Yuce 2018-8-24
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 个评论
mz123
mz123 2019-1-4
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 2019-1-7
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.

请先登录,再进行评论。

更多回答(2 个)

Jan
Jan 2012-2-7
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 个评论
Matthew Worker
Matthew Worker 2018-4-25
Hi yes, I have this issue and unable to solve it? In Matlab 2018a has there been a solution?

请先登录,再进行评论。


Rueben Mendelsberg
Rueben Mendelsberg 2020-2-25
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.

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by