0

我在 JScrollPane 中有一个 JTable,我想以编程方式滚动到表格的底部。我试过的代码是:

int bottomRow = table.getRowCount()-1;
Rectangle rect = table.getCellRect(bottomRow,0,true);
table.scrollRectToVisible(rect);

我也尝试了代码:

int bottomRow = table.getRowCount()-1;
Rectangle rect = table.getCellRect(bottomRow,0,true);
jscrollPane1.getViewPort().setViewPosition(rect.getLocation());

两个代码片段的行为相同,并且都不是将表格滚动到底行,而是根据矩形的高度将表格滚动到底行上方的几行。

我需要帮助才能在可见矩形中查看表格的最后一行。

4

2 回答 2

2

疯狂猜测(因为您没有提供足够的上下文)您想要在收到有关 tableModel 更改的通知时更新滚动值。

在这种情况下,问题在于表本身正在侦听模型以更新其内部。当您想根据表本身的状态更改某些内容时,您必须确保您的操作仅内部完全更新后才会发生,例如:

public void tableChanged(TableModelEvent e) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            // scroll to last row
        }
    });
}
于 2013-09-03T08:49:19.630 回答
0

我写了这个简单的例子来演示一个可行的解决方案,看到这个问题没有进一步的发展,我将它作为一个工作示例发布,希望它可能会提示来自 OP 的更多信息

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class ToLastRow {

    public static void main(String[] args) {
        new ToLastRow();
    }

    public ToLastRow() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                DefaultTableModel model = new DefaultTableModel(new Object[]{"Look no hands..."}, 0);
                for (int index = 0; index < 1000; index++) {
                    model.addRow(new Object[]{index});
                }

                final JTable table = new JTable(model);

                JButton last = new JButton("Last");
                JButton first = new JButton("First");

                last.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        int row = table.getRowCount() - 1;
                        scrollTo(table, row);
                    }
                });
                first.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        scrollTo(table, 0);
                    }
                });

                JPanel buttons = new JPanel();
                buttons.add(last);
                buttons.add(first);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(table));
                frame.add(buttons, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public void scrollTo(JTable table, int row) {
        Rectangle bounds = table.getCellRect(row, 0, true);
        table.scrollRectToVisible(bounds);
        table.addRowSelectionInterval(row, row);
    }
}
于 2013-09-04T01:53:57.557 回答