Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Set uitable scroll position

719 views
Skip to first unread message

coffee_pot

unread,
May 17, 2011, 3:40:05 PM5/17/11
to
I have a uitable where I add or remove items by changing the Data property. Of course, every time I do this, the uitable automatically resets to focus on the top row of data.

Is there a way I can set the scroll position for the table such that I can prevent the view "jumping" every time the table is updated?

I understand nothing of the sort is currently supported by matlab, but does anyone know how I can achieve this given access to the underlying java swing object? I have already achieved, in other respects, the functionality I desire from the uitable using the findjobj function from the file exchange.

Yair Altman

unread,
Jun 5, 2011, 5:34:04 PM6/5/11
to
"coffee_pot" wrote in message <iquiuk$j40$1...@newscl01ah.mathworks.com>...

> I have a uitable where I add or remove items by changing the Data property. Of course, every time I do this, the uitable automatically resets to focus on the top row of data.
>
> Is there a way I can set the scroll position for the table such that I can prevent the view "jumping" every time the table is updated?
>
> I understand nothing of the sort is currently supported by matlab, but does anyone know how I can achieve this given access to the underlying java swing object? I have already achieved, in other respects, the functionality I desire from the uitable using the findjobj function from the file exchange.


There are two solutions. Note that both of them are unsupported and undocumented. Both solutions require the FINDJOBJ utility ( http://www.mathworks.com/matlabcentral/fileexchange/14317-findjobj-find-java-handles-of-matlab-graphic-objects ) to access the uitable's underlying Java table object reference handle:

1. Rather than updating the entire data, update only a particular table cell, as explained for example here: http://www.mathworks.com/matlabcentral/newsreader/view_thread/308624 (there are several other examples if you search the newsgroup for "uitable" and "findjobj" and/or "setValueAt"

2. Control the scrollbars, as explained here:

http://undocumentedmatlab.com/blog/setting-line-position-in-edit-box-uicontrol/
http://undocumentedmatlab.com/blog/customizing-listbox-editbox-scrollbars/

Yair Altman
http://UndocumentedMatlab.com

coffee_pot

unread,
Jun 6, 2011, 6:34:04 AM6/6/11
to
Yair,

Thanks very much for your response. I think your second suggestion would work best for my situation, but I don't can't see how to apply it to my problem. Though the EditBox article changes the line position, it does this with the setCaretPosition method. I don't quite see how this could work with the uitable, and the other article dealt with the Scrollbar policies and line-wrapping, but didn't mention any way to get or set the current scroll position.

Is there a way once I've got the Java scroll-pane container reference that I can

1) Get the current line position corresponding to the top visible row
2) Set the scroll position to match that in (1)

Thanks,

Andy

"Yair Altman" wrote in message <isgsoc$fof$1...@newscl01ah.mathworks.com>...

María

unread,
Jan 10, 2013, 11:02:08 AM1/10/13
to
Hi,
I have the same problem: I want to prevent the jumping to the top row after updating the the 'Data' property of the table.

I want to update the position (row_num, 5) of the uitable, which contains a logical value. I've tried the following:

number=logical(1);
row_num=2;
jscroll = findjobj(handles.tabla_estructura);
jtable = jscroll.getViewport.getComponent(0);
jtable.setValueAt(number,row_num-1,4);

When I try to execute that code inside the CellEditCallback it seems to call itself (CellEditCallback is called again). If I execute the code in other place, I get lots of zeros:
ans =
0
ans =
0
ans =
0
[...]

Any ideas?

Thanks a lot!

María

unread,
Jan 15, 2013, 1:52:08 AM1/15/13
to
Fogot my last message, I was loading my data erroneously, and that is why I couln't edit it. Sorry.

"María" wrote in message <kcmom0$ovp$1...@newscl01ah.mathworks.com>...

Daniel

unread,
Apr 14, 2015, 6:58:53 PM4/14/15
to
I found this to be a workable approach along the lines of what's already been suggested, but it does have one issue I don't know how to solve.


jTable = findjobj(hTable); % hTable is the handle to the uitable object
jScrollPane = jTable.getComponent(0);
javaObjectEDT(jScrollPane); % honestly not sure if this line matters at all
currentViewPos = jScrollPane.getViewPosition; % save current position
set(hTable,'data',newData); % resets the vertical scroll to the top of the table
drawnow; % without this drawnow the following line appeared to do nothing
jScrollPane.setViewPosition(currentViewPos);% reset the scroll bar to original position


I found that I had to insert a drawnow as above or the setViewPosition didn't do anything (some race condition between the java EDT and the execution thread?). With the drawnow in place the scroll bar resets to the top of the table temporarily before returning to its original position where it had been prior to set(hTable,'data',newData). The back and forth is visually jarring but better than not returning to original position at all.

Does anyone have a solution to this back-and-forth scrolling?

"coffee_pot" wrote in message <isiaes$25g$1...@newscl01ah.mathworks.com>...

Zaki

unread,
Sep 8, 2015, 2:38:08 AM9/8/15
to
"Daniel" wrote in message <mgk634$jsc$1...@newscl01ah.mathworks.com>...

> Does anyone have a solution to this back-and-forth scrolling?

Thanks Daniel for that input, that worked for me. Did you manage to find the answer to the back-and-forth issue?

Daniel

unread,
Sep 8, 2015, 1:38:19 PM9/8/15
to
Hi Zaki,

No I have not found a solution to the back-and-forth problem.

dan

"Zaki" <zaki.m...@bosch.com> wrote in message <mslvoa$2c8$1...@newscl01ah.mathworks.com>...

Daniel Cunha

unread,
Sep 22, 2015, 2:58:14 PM9/22/15
to
"Daniel" wrote in message <msn6e3$oss$1...@newscl01ah.mathworks.com>...
Hello guys,

I don't really know if this is an efficient way to solve this, but if you substitute "drawnow" for "pause(0)", the scroll position updates will occur faster and instead of a back-and-forth scrolling, the table should just blink once.

jTable = findjobj(hTable);
jScrollPane = jTable.getComponent(0);
scrollPos = jScrollPane.getViewPosition;
% % %
> do any stuff you want with your table ...
% % %
pause(0);
jScrollPane.setViewPosition(scrollPos);

Daniel Cunha

unread,
Sep 22, 2015, 3:32:15 PM9/22/15
to
Actually, I just figured it out that this approach can not guarantee a correct result!
While pause is running, updates will happen, but not always the updates we want... My codes were working only because I am lucky, so no, I could not solve this back-and-forth scrolling neither. I am sorry for my hurry on the last reply.

"Daniel Cunha" wrote in message <mts8br$mis$1...@newscl01ah.mathworks.com>...

John

unread,
Oct 1, 2015, 11:15:11 AM10/1/15
to
"Daniel Cunha" wrote in message <mtsabj$qt7$1...@newscl01ah.mathworks.com>...

John

unread,
Oct 1, 2015, 11:18:09 AM10/1/15
to
Hi Guys,

I face the same problem with the scroll position in the uitable and i am glad i found this thread here.

Unfortunately i dont know where to integrate this piece of code

jTable = findjobj(hTable); % hTable is the handle to the uitable object
jScrollPane = jTable.getComponent(0);
javaObjectEDT(jScrollPane); % honestly not sure if this line matters at all
currentViewPos = jScrollPane.getViewPosition; % save current position
set(hTable,'data',newData); % resets the vertical scroll to the top of the table
drawnow; % without this drawnow the following line appeared to do nothing
jScrollPane.setViewPosition(currentViewPos);% reset the scroll bar to original position

Can somebody help me with this please?

My uitables callback looks like this:

function uitable__CellEditCallback(hObject, eventdata, handles)
% hObject handle to uitable_ (see GCBO)
% eventdata structure with the following fields (see UITABLE)
% Indices: row and column indices of the cell(s) edited
% PreviousData: previous data for the cell(s) edited
% EditData: string(s) entered by the user
% NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
% Error: error string when failed to convert EditData to appropriate value for Data
% handles structure with handles and user data (see GUIDATA)

data=get(hObject,'Data'); % get the data cell array of the table
cols=get(hObject,'ColumnFormat'); % get the column formats

if strcmp(cols(eventdata.Indices(2)),'logical') % if the column of the edited cell is logical
if eventdata.EditData % if the checkbox was set to true
data{eventdata.Indices(1),eventdata.Indices(2)}=true; % set the data value to true
else % if the checkbox was set to false
data{eventdata.Indices(1),eventdata.Indices(2)}=false; % set the data value to false
end
end
set(hObject,'Data',data); % now set the table's data to the updated data cell array


I would be glad for your help!

Best regards, John

"Daniel Cunha" wrote in message <mtsabj$qt7$1...@newscl01ah.mathworks.com>...

Daniel

unread,
Oct 1, 2015, 1:18:14 PM10/1/15
to
Hi John,

You can integrate this solution into your code as follows:
...
if strcmp(cols(eventdata.Indices(2)),'logical') % if the column of the edited cell is logical
if eventdata.EditData % if the checkbox was set to true
data{eventdata.Indices(1),eventdata.Indices(2)}=true; % set the data value to true
else % if the checkbox was set to false
data{eventdata.Indices(1),eventdata.Indices(2)}=false; % set the data value to false
end
end

jTable = findjobj(hObject); % hTable is the handle to the uitable object
jScrollPane = jTable.getComponent(0);
javaObjectEDT(jScrollPane); % honestly not sure if this line matters at all
currentViewPos = jScrollPane.getViewPosition; % save current position

set(hObject,'Data',data); % now set the table's data to the updated data cell array

drawnow; % without this drawnow the following line appeared to do nothing
jScrollPane.setViewPosition(currentViewPos);% reset the scroll bar to original position

"John" wrote in message <mujirb$59o$1...@newscl01ah.mathworks.com>...

John

unread,
Oct 2, 2015, 4:51:17 AM10/2/15
to
Thank you very much Daniel for your help!!!:) I really appreciate that! it works perfectly!!
Best regards, John


"Daniel" wrote in message <mujpsb$k5k$1...@newscl01ah.mathworks.com>...

Ana

unread,
Oct 13, 2015, 10:37:15 AM10/13/15
to
Hello,

I am experiencing a very similar problem. With the help of this thread I was able to reset the scroll position to the point I want it, but I want the cell to be editable after this code and I cannot manage to do it.

My specific problem is: I have a hint text on a specific editable (text) cell of a table. When I click this cell I want the text to disappear but still I want the cursor to be in the cell to introduce the new value. I have done the first part, but to be able to write on this cell I need to click again, and it would be good if I didn't have to do it.

Can anyone help?

Thanks in advance,

Ana

"Daniel" wrote in message <mujpsb$k5k$1...@newscl01ah.mathworks.com>...

Joe

unread,
Dec 15, 2015, 1:31:20 PM12/15/15
to
Hey all,

I was looking through this thread and wondered if there was another way to control a listbox's scroll position.

I have Matlab 2015a, and I found that my listbox has a property called "ListboxTop"

When I change this value, it changed the position of the scroll bar.

Hopefully this can be helpful to someone!

Marc

unread,
Dec 15, 2015, 1:36:45 PM12/15/15
to
"Joe" wrote in message <n4ab17$2ci$1...@newscl01ah.mathworks.com>...
This is working for me for editable table.

jTable = findjobj(Htable);
jScrollPane = jTable.getComponent(0);
scrollPos = jScrollPane.getViewPosition;
set(Htable,'data',data)
drawnow;
jScrollPane.setViewPosition(scrollPos);

Bruno Luong

unread,
Dec 15, 2015, 1:45:45 PM12/15/15
to
"Joe" wrote in message <n4ab17$2ci$1...@newscl01ah.mathworks.com>...
> Hey all,
>
> I was looking through this thread and wondered if there was another way to control a listbox's scroll position.
>
> I have Matlab 2015a, and I found that my listbox has a property called "ListboxTop"
>
> When I change this value, it changed the position of the scroll bar.

This thread is about uitable not listbox

Lockywolf

unread,
Mar 15, 2016, 10:54:07 AM3/15/16
to
"Marc" wrote in message <n4h95b$rho$1...@newscl01ah.mathworks.com>...
Just tried the same method.

It works, yes, but has a drawback - it is very slow. Cannot use it, unfortunately.

I tried retaining the jTable between the table updates, but it seems that is is actually necessary to have a new jTable every time we call a table modification function.

:-(

Harry Chen

unread,
Aug 15, 2017, 5:17:20 AM8/15/17
to
Hi,

Is there a way to set the view position arbitrarily rather than the previous view position? In my case, the position is determined by the mouse clicking on an image plot.

Any advice is appreciated.

H. Chen
0 new messages