我有一个显示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。如果看到代码的任何其他部分可以帮助我告诉我,并且抱歉没有将其发布到开头。
答案 0 :(得分:1)
我发布的渲染器没有问题。您可以在底部查看我的代码。我所能建议的只是
不要将尺寸设置为任何。学习使用布局管理器,只需打包您的框架,以便尊重所有首选尺寸。
即使在您的渲染器中,也不要设置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();
}
});
}
}
答案 1 :(得分:0)
我通过遍历每一行并累加行高来解决此问题,然后将表格的首选大小设置为总高度。
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);
}