gpt4 book ai didi

java - JScrollPane 不会滚动到 JTable 的底部

转载 作者:行者123 更新时间:2023-11-30 04:02:48 25 4
gpt4 key购买 nike

我有一个显示 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 中很明显存在具有不同高度的行。目前该表的外观如下:

enter image description here

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

最佳答案

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

  • 请勿将尺寸设置为任何内容。学习使用布局管理器并打包框架,以便尊重所有首选尺寸。

  • 即使在渲染器中,也不要设置 JTextArea 的大小,而是使用此构造函数

    JTexArea(String text, int rows, int columns)
  • 不要使用空布局。我也遇到过类似的问题,有人试图在 table 上设置界限。

<小时/>
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();
}
});
}
}

关于java - JScrollPane 不会滚动到 JTable 的底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21534958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com