How to control Uitable scroller position?
12 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Can Yuce
on 24 Aug 2018
Edited: Can Yuce
on 24 Aug 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 Comments
mz123
on 4 Jan 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
on 7 Jan 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.
More Answers (2)
Jan
on 7 Feb 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 Comments
Matthew Worker
on 25 Apr 2018
Hi yes, I have this issue and unable to solve it? In Matlab 2018a has there been a solution?
Rueben Mendelsberg
on 25 Feb 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.
0 Comments
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!