如何在JTable中动态设置RowHeight [英] How to set the RowHeight dynamically in a JTable
问题描述
我想在 JTable
中放置一个比给定单元格宽度更长的String。
如何动态设置 rowHeight
以便我可以读取整个字符串?
这是一个例子:
I want to put a String in a JTable
that is longer than the given cell-width.
How can I set the rowHeight
dynamically so that I can read the whole String?
Here is an example:
import javax.swing.*;
public class ExampleTable {
public JPanel createTable() {
JPanel totalGUI = new JPanel();
//define titles for table
String[] title = {"TITLE1", "TITLE2", "TITLE3"};
//table data
Object[][] playerdata = {
{new Integer(34), "Steve", "test test test"},
{new Integer(32), "Patrick", "dumdi dumdi dummdi dumm di di didumm"},
{new Integer(10), "Sarah", "blabla bla bla blabla bla bla blabla"},};
//create object 'textTable'
JTable textTable = new JTable(playerdata,title);
//set column width
textTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
textTable.getColumnModel().getColumn(0).setPreferredWidth(60);
textTable.getColumnModel().getColumn(1).setPreferredWidth(60);
textTable.setDefaultRenderer(String.class, new RowHeightCellRenderer());
//scrollbar
JScrollPane scrollPane = new JScrollPane(textTable);
totalGUI.add(scrollPane);
return totalGUI;
}
private static void createAndShowGUI() {
//create main frame
JFrame mainFrame = new JFrame("");
ExampleTable test = new ExampleTable();
JPanel totalGUI = new JPanel();
totalGUI = test.createTable();
//visible mode
mainFrame.add(totalGUI); //integrate main panel to main frame
mainFrame.pack();
mainFrame.setVisible(true);
}
public static void main (String[] args) {
createAndShowGUI();
}//main
}
在这里你' ll查看代码,该代码对给定单元格的每个文本进行换行
And here you'll see the code which line-breaks each text that is to long for the given cell
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class RowHeightCellRenderer extends JTextArea implements TableCellRenderer
{
/**
*
*/
private static final long serialVersionUID = 1L;
public Component getTableCellRendererComponent (JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column ) {
setText( value.toString() );
return this;
}
}
谢谢,但我想动态实现RowHeight,取决于字符串长度...
我想读取单元格中的整个字符串/文本。有什么建议吗?
thank you but I want to implement the RowHeight dynamically, depending on String length... I want to read the whole String/text in the cell. any suggestions?
我是java初学者,这是我的第一个问题。我很高兴得到答案。
I'm java beginner and this is my first question. I would be delighted I get an answer.
推荐答案
使用JTextArea作为渲染组件时有几个问题(如果不是所有这些都已在本网站的几个QA中解释过了。试图总结一下:
There are several issues when using a JTextArea as rendering component (and most if not all of them already explained in several QA's on this site). Trying to sum them up:
根据渲染组件的大小要求调整单个行高
基本上,要走的方法是根据需要循环遍历单元格,然后
Basically, the way to go is to loop through the cells as needed, then
- 使用数据<配置其渲染器
- 询问渲染组件的首选大小
- 将表格行高设置为pref height
OP编辑过的问题中的updateRowHeight方法很好。
The updateRowHeight method in the OP's edited question is just fine.
JTextArea计算其preferredSize
为了获得一个合理的尺寸提示,它需要在另一个维度上以合适的尺寸进行播种。也就是说,如果我们想要一个需要宽度的高度,那么必须在每个调用中完成。在表的上下文中,合理的宽度是当前列宽:
to get a reasonable sizing hint for one dimension, it needs to be "seeded" with some reasonable size in the other dimension. That is if we want the height it needs a width, and that must be done in each call. In the context of a table, a reasonable width is the current column width:
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
... // configure visuals
setText((String) value);
setSize(table.getColumnModel().getColumn(column).getWidth(),
Short.MAX_VALUE);
return this;
}// getTableCellRendererComponent
动态调整高度
在表/列/模型的某些稳定状态下完全确定的行高。因此,在初始化完成后以及任何其所依赖的状态发生更改时,您都要设置它(调用updateRowHeight)一次。
The row height it fully determined in some steady state of the table/column/model. So you set it (call updateRowHeight) once after the initialization is completed and whenever any of the state it depends on is changed.
// TableModelListener
@Override
public void tableChanged(TableModelEvent e) {
updateRowHeights();
}
// TableColumnModelListener
@Override
public void columnMarginChanged(ChangeEvent e) {
updateRowHeights();
}
注意
作为一般规则,getXXRendererComponent中的所有参数都是严格只读的,实现不得更改调用者的任何状态。从渲染器中更新rowHeight 错误。
As a general rule, all parameters in the getXXRendererComponent are strictly read-only, implementations must not change any state of the caller. Updating the rowHeight from within the renderer is wrong.
这篇关于如何在JTable中动态设置RowHeight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!