如何使用键盘箭头键同步行 header JTable 滚动与另一个 table

How to synchronize row header JTable scrolling with another table using keyboard arrow keys

在组件 class 中,我有 2 个 JTables,一个是固定行 header table,另一个是数据 table,其中包含行 header。我为数据 table 设置了一个 JScrollPane,这样它将同时滚动两个 tables.

在另一个 class 中,我有一个 JPanel,我在上面添加了要显示的组件 class。

在面板中,当我使用鼠标滚轮或单击滚动条滚动时,tables 的滚动很好。但是,如果我 select 行 header 并按下键盘上的向下或向上箭头键,我注意到只有行 header 继续滚动,但数据 [=33= 】 不动。因此,tables 行不再正确对齐。

在另一个注释中,我注意到如果我 select 数据 table 行改为使用 up/down 箭头键滚动,两个 table 都可以同步正确滚动!

有人知道为什么会这样吗?如果 header table 行被 select 编辑,我该如何解决滚动问题?

查看 Fixed Column Table

那里的逻辑使用 ChangeListener 来使行 header 的滚动与滚动窗格的垂直滚动条同步。

基本代码为:

scrollPane.getRowHeader().addChangeListener( this );

和听众:

public void stateChanged(ChangeEvent e)
{
    //  Sync the scroll pane scrollbar with the row header

    JViewport viewport = (JViewport) e.getSource();
    scrollPane.getVerticalScrollBar().setValue(viewport.getViewPosition().y);
}