简体   繁体   English

JTable不会滚动到底行

[英]JTable does not scroll to bottom row

I have a JTable with in a JScrollPane, I want to scroll to the bottom of the table programatically. 我在JScrollPane中有一个JTable,我想以编程方式滚动到表的底部。 The code I tried is: 我试过的代码是:

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

Also I tried the code: 我也尝试了代码:

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

Both the code snippets behave the same and both are scrolling the table not to the bottom row but a few rows above the bottom row depending upon the height of the rectangle. 两个代码片段的行为都相同,并且两者都滚动表格而不是底行,而是根据矩形的高度在底行上方几行。

I need help to see the last row of the table in visible rectangle. 我需要帮助才能在可见矩形中看到表格的最后一行。

Wildly guessing (as you didn't provide enough context) that you want to update the scroll value when notified about a change in the tableModel. 疯狂猜测(因为您没有提供足够的上下文),当您收到有关tableModel中的更改的通知时,您希望更新滚动值。

In this case the problem is that the table itself is listening to the model to update its internals. 在这种情况下,问题是表本身正在监听模型以更新其内部。 As you want to change something based on state of the table itself, you have to ensure that your action happens only after the internals are completely updated, something like: 由于您希望根据表本身的状态更改某些内容,因此必须确保仅内部完全更新后才会执行操作,例如:

public void tableChanged(TableModelEvent e) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            // scroll to last row
        }
    });
}

I wrote this simple example to demonstrate a workable solution, seen there has being no further development with the question, I'll post it as a working example in the hope that it might prompt more info from the OP 我写了这个简单的例子来演示一个可行的解决方案,看到这个问题没有进一步的发展,我会把它作为一个工作的例子发布,希望它可以从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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM