7

I am trying to create a table in MATLAB where I can change the scroll position programmatically. I created a uitable and extracted the handle of the Java UITablePeer object (designated here as htable).

I then got the handle for the UIScrollbarPane object using:

hscroll = htable.getParent.getParent.getVerticalScrollBar

At this point, the figure looks like this (my actual table is more complicated, this is just something I made as an example):

before

The box at the lower left corner of the table is a pushbutton. When its callback is triggered, it uses the setValue method to change the scrollbar location, e.g.

hscroll.setValue(10)

After this, the table looks like this:

After

As you can see, the top of the table becomes corrupted. I've tried using the refresh function or repaint method, but they don't seem to help; the only way to "fix" this is to move another window (by dragging or Alt-Tabbing) such that the table is completely covered, and when I switch back to the table it's OK. Obviously, this isn't really an optimal solution.

Can anyone suggest a way to prevent or fix this?

Thanks

4

1 回答 1

4

这是我的实现(改编自此代码):

% create a sample table with random data
figure('Menubar','none', 'Position',[400 400 250 300])
h = uitable('Units','normalized', 'Position',[0 0 1 1], ...
    'ColumnName',{'1','2'}, 'Data',num2cell(rand(50,2)>0.5));

% get Java handles
jScroll = findjobj(h, 'class','UIScrollPane');
jView = jScroll.getViewport();
jTable = jView.getView();

% scroll to specified row (make it the top row)
row = 20 - 1;        % 20th row (zero-based index)
jView.setViewPosition(java.awt.Point(0, row * jTable.getRowHeight()))
jScroll.repaint()    % workaround for any visual glitches

每次我尝试时,表格都会正确显示(没有故障):

滚动表

于 2014-08-01T18:52:51.290 回答