JScrollPane不会滚动到JTable的底部

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JScrollPane不会滚动到JTable的底部相关的知识,希望对你有一定的参考价值。

我有一个显示JTable的JScrollPane,并且JTable根据需要多少行来调整每个单元格中的文本,具有不同的行高度,如此自定义所定义

DefaultTableCellRenderer renderer = new DefaultTableCellRenderer() {

    public Component getTableCellRendererComponent (JTable table, 
                Object obj, boolean isSelected, boolean hasFocus, int row, int column) {

            Component rendererComp = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus,
                    row, column);
            if(column == 5) {
                JTextArea text = new JTextArea(obj.toString());
                text.setFont(rendererComp.getFont());
                text.setLineWrap(true);
                text.setWrapStyleWord(true);
                text.setSize(new Dimension(
                        moves.getColumnModel().getColumn(column).getWidth(),
                        9999999));
                text.setSize(text.getSize().width, text.getPreferredSize().height);
                table.setRowHeight(row, text.getPreferredSize().height);
                return text;
            } else 
                return rendererComp;
        }
    };

只有第五列才能获得文本区域,因为只有第五列可以有足够的文本才能需要多行。

我加入这个原因的原因是我添加这个渲染器之前JScrollPane工作正常,但这需要是单元格渲染器,因为否则JTable中的某些信息根本就不会显示。它适用于在正确的高度创建行。但是,JTable的preferredSize显然计算错误(我确定它不是,而且我搞砸了但看起来就是这样),因为现在JScrollPane不会一直滚动到底部。在为每列设置单元格渲染器后,我在JTable的每一行上调用getRowHeight时,它们都返回16,尽管JTable中的行高度非常明显。这是目前表格的样子:

正如您所看到的,应该显示下一行的条子,但不能是因为滚动条实际上​​并不是一直向下滚动。我已经设置了JTable和JScrollPane的首选大小,我甚至通过将任意常量添加到JTable的首选大小的高度来显示整个表,但程序需要能够计算出正确的大小基于表中的信息。我只想知道如何在JScrollPane中准确显示整个JTable。如果看到代码的任何其他部分可以帮助我告诉我,并抱歉没有发布它开始。

答案

我发布的渲染器没有问题。您可以在底部查看我的代码。我只能建议

  • 不要将尺寸设置为任何东西。学习使用布局管理器,只需打包您的框架,以便尊重所有首选尺寸。
  • 即使在渲染器中,也不要设置JTextArea的大小而是使用此构造函数 JTexArea(String text, int rows, int columns)
  • 不要使用null布局。有人试图在桌子上设置边界时,我看到了类似的问题。

import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;

public class Test {

    public Test() {
        String[][] data = {
            {"sdvjdnvdfjvnsdvsdjvs", "sdvjdnvdfjvnsdvsdjvsdvkdsjvnbdvlkjvnbdkvdvkdjvvsnvlkdnv"},
            {"sdvjdnvdfjvnsdvsdjvs", "sdvjdnvdfjvnsdvsdjvsdvkdsjvnbdvlkjvnbdkvdvkdjvvsnvlkdnv"},
            {"sdvjdnvdfjvnsdvsdjvs", "sdvjdnvdfjvnsdvsdjvsdvkdsjvnbdvlkjvnbdkvdvkdjvvsnvlkdnv"},
            {"sdvjdnvdfjvnsdvsdjvs", "sdvjdnvdfjvnsdvsdjvsdvkdsjvnbdvlkjvnbdkvdvkdjvvsnvlkdnv"},
            {"sdvjdnvdfjvnsdvsdjvs", "sdvjdnvdfjvnsdvsdjvsdvkdsjvnbdvlkjvnbdkvdvkdjvvsnvlkdnv"},
            {"sdvjdnvdfjvnsdvsdjvs", "sdvjdnvdfjvnsdvsdjvsdvkdsjvnbdvlkjvnbdkvdvkdjvvsnvlkdnv"},
            {"sdvjdnvdfjvnsdvsdjvs", "sdvjdnvdfjvnsdvsdjvsdvkdsjvnbdvlkjvnbdkvdvkdjvvsnvlkdnv"},
            {"sdvjdnvdfjvnsdvsdjvs", "sdvjdnvdfjvnsdvsdjvsdvkdsjvnbdvlkjvnbdkvdvkdjvvsnvlkdnv"},
            {"sdvjdnvdfjvnsdvsdjvs", "sdvjdnvdfjvnsdvsdjvsdvkdsjvnbdvlkjvnbdkvdvkdjvvsnvlkdnv"},
            {"sdvjdnvdfjvnsdvsdjvs", "sdvjdnvdfjvnsdvsdjvsdvkdsjvnbdvlkjvnbdkvdvkdjvvsnvlkdnv"},
            {"sdvjdnvdfjvnsdvsdjvs", "sdvjdnvdfjvnsdvsdjvsdvkdsjvnbdvlkjvnbdkvdvkdjvvsnvlkdnv"},
            {"sdvjdnvdfjvnsdvsdjvs", "sdvjdnvdfjvnsdvsdjvsdvkdsjvnbdvlkjvnbdkvdvkdjvvsnvlkdnv"},
            {"sdvjdnvdfjvnsdvsdjvs", "sdvjdnvdfjvnsdvsdjvsdvkdsjvnbdvlkjvnbdkvdvkdjvvsnvlkdnv"},
            {"sdvjdnvdfjvnsdvsdjvs", "sdvjdnvdfjvnsdvsdjvsdvkdsjvnbdvlkjvnbdkvdvkdjvvsnvlkdnv"},
            {"sdvjdnvdfjvnsdvsdjvs", "sdvjdnvdfjvnsdvsdjvsdvkdsjvnbdvlkjvnbdkvdvkdjvvsnvlkdnv"},
            {"sdvjdnvdfjvnsdvsdjvs", "sdvjdnvdfjvnsdvsdjvsdvkdsjvnbdvlkjvnbdkvdvkdjvvsnvlkdnv"},
            {"sdvjdnvdfjvnsdvsdjvs", "sdvjdnvdfjvnsdvsdjvsdvkdsjvnbdvlkjvnbdkvdvkdjvvsnvlkdnv"},
            {"sdvjdnvdfjvnsdvsdjvs", "sdvjdnvdfjvnsdvsdjvsdvkdsjvnbdvlkjvnbdkvdvkdjvvsnvlkdnv"},
            {"sdvjdnvdfjvnsdvsdjvs", "sdvjdnvdfjvnsdvsdjvsdvkdsjvnbdvlkjvnbdkvdvkdjvvsnvlkdnv"},
            {"sdvjdnvdfjvnsdvsdjvs", "sdvjdnvdfjvnsdvsdjvsdvkdsjvnbdvlkjvnbdkvdvkdjvvsnvlkdnv"},
            {"sdvjdnvdfjvnsdvsdjvs", "sdvjdnvdfjvnsdvsdjvsdvkdsjvnbdvlkjvnbdkvdvkdjvvsnvlkdnv"}};

        String[] cols = {"COL 1", "COL 2"};

        final JTable moves = new JTable(data, cols);

        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer() {

            public Component getTableCellRendererComponent(JTable table,
                    Object obj, boolean isSelected, boolean hasFocus, int row, int column) {

                Component rendererComp = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus,
                        row, column);
                if (column == 1) {
                    JTextArea text = new JTextArea(obj.toString(), 2, 20);
                    text.setFont(rendererComp.getFont());
                    text.setLineWrap(true);
                    text.setWrapStyleWord(true);
                    text.setSize(text.getSize().width, text.getPreferredSize().height);
                    table.setRowHeight(row, text.getPreferredSize().height);
                    return text;
                } else {
                    return rendererComp;
                }
            }
        };

        moves.setDefaultRenderer(Object.class, renderer);

        JFrame frame = new JFrame();
        frame.add(new JScrollPane(moves));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Test test = new Test();
            }
        });
    }
}
另一答案

我通过迭代每一行并对行高进行求和来解决这个问题,然后将表首选大小设置为总高度。

public final void updateTablePreferredSize ( JTable table )
{
    int totalRowHeight = 0;

    for ( int row = 0; row < table.getRowCount(); row++ )
    {
        totalRowHeight +=  table.getRowHeight(row);
    }

    Dimension dimension = table.getPreferredSize();
    dimension.setSize ( dimension.getWidth(), totalRowHeight );
    table.setPreferredSize(dimension);
}

以上是关于JScrollPane不会滚动到JTable的底部的主要内容,如果未能解决你的问题,请参考以下文章

JTable 滚动到指定的行索引

JScrollPane 中的 JTable:禁用滚动条的最佳高度

如何使 JTable 自动调整大小和水平滚动?

如何刷新与 jtable 绑定的 Jscrollpane 中的数据?

如何触发jScrollPane滚动到底部事件

Java Swing通过拖动滚动JScrollPane