刷新Jtable会滚动表格

时间:2013-07-24 07:27:39

标签: java swing jtable jscrollpane

当我使用自定义刷新按钮刷新jtable数据时,jtable滚动到随机位置。 我使用的代码是:

JscrollPane jsPane = new JscrollPane();
Jtable table = new JTable();
jsPane.setViewportView(table);
getContentPane().add(jsPane, java.awt.BorderLayout.CENTER);

我希望jtable在刷新后保持其视口。 我怎么能实现这个目标呢?

1 个答案:

答案 0 :(得分:1)

正如克利奥帕特拉所说,非常奇怪。 但你仍然可以轻易强迫:

int scrollPosition = scroll.getVerticalScrollBar().getValue();
try {
...do the update to the table...
} finally {
    scroll.getVerticalScrollBar().setValue(scrollPosition);
    }
}
相关问题