How to control Uitable scroller position?
조회 수: 14 (최근 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.
댓글 수: 0
채택된 답변
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
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
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
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
2018년 4월 25일
Hi yes, I have this issue and unable to solve it? In Matlab 2018a has there been a solution?
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!