如何使用单个滚动条滚动两个或多个JTable? [英] How to scroll two or more JTables with a single Scrollbar?
本文介绍了如何使用单个滚动条滚动两个或多个JTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用一个JScrollPane
中的一个滚动移动另一个或多个JScrollPane
?
How can I use one scroll from one JScrollPane
to move another or more than one JScrollPane
?
例如:
我在单独的JScrollPane
中有三个JTable
.我想将滚动窗格彼此绑定.
I have three JTable
s in separate JScrollPane
s. I want to bind the scrollpanes to each other.
如果我要使用一个-另一个将以相同的方式滚动.
If I'll use one - the another will scroll the same way.
我找不到某些Listener
吗?
有没有建议?
最诚挚的问候.
推荐答案
保留JTables
'标头的方法是对每个JScrollPane
的垂直滚动条使用相同的BoundedRangeModel
并将每个ScrollPane添加到一个JPanel
.
An approach that preserves the JTables
' headers would be to use the same BoundedRangeModel
for each JScrollPane
's vertical scrollbar and add each ScrollPane to a single JPanel
.
class ParallelTables {
static JScrollPane createTable() {
DefaultTableModel model = new DefaultTableModel(100, 2);
for (int row=model.getRowCount(); --row>=0;) {
model.setValueAt(row, row, 0);
}
JTable table = new JTable(model);
return new JScrollPane(table);
}
public static void main(String[] args) throws Exception {
JScrollPane scrollerA = createTable();
JScrollPane scrollerB = createTable();
scrollerA.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_NEVER);
// the following statement binds the same BoundedRangeModel to both vertical scrollbars.
scrollerA.getVerticalScrollBar().setModel(
scrollerB.getVerticalScrollBar().getModel());
JPanel panel = new JPanel();
panel.add(scrollerA);
panel.add(scrollerB);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
参考:
这篇关于如何使用单个滚动条滚动两个或多个JTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文