简体   繁体   English

如何根据在单元格中输入的文本内容增加jtable中行的高度

[英]How to increase the height of a row in jtable depending on the text content entered in its cell

I have a jtable which is editable. 我有一个可编辑的jtable。 As and when the cells are written with the text by user,if the entered text is more to fit the size of cell then the height of the row in table must increase to fit the new text entered by user. 当用户使用文本写入单元格时,如果输入的文本更符合单元格的大小,则表格中行的高度必须增加以适合用户输入的新文本。 Could you tell me how to increase the height of the row depending on the lines of text entered. 你能告诉我如何根据输入的文本行增加行的高度。

Can anyone help me in this? 任何人都可以帮助我吗? I have tried to add JTextArea into a row, but this is not working. 我试图将JTextArea添加到一行,但这不起作用。

You will need to calculate the needed height for the newly entered text. 您需要计算新输入文本所需的高度。 Depending on the renderer you are using for the cell, you can use eg JTextArea.getLineCount() to find out how many lines the text contains. 根据您用于单元格的渲染器,您可以使用例如JTextArea.getLineCount()来查找文本包含的行数。

Then you need to retrieve the height of the font used by the renderer and calculate the needed height out of that. 然后,您需要检索渲染器使用的字体高度,并计算出所需的高度。

Once you have that number, use JTable.setRowHeight(int, int) to change the height for the specific row. 获得该数字后,使用JTable.setRowHeight(int, int)更改特定行的高度。

Something like this: 像这样的东西:

Component c = renderer.getTableCellRendererComponent(theTable, theTable.getValueAt(row, col), false, false, row, col);
Font f = c.getFont();
FontMetrics fm = c.getFontMetrics(f);

// this cast depends on the way your renderer is implemented !!!!
int lineCount = ((JTextArea)c).getLineCount();

tnt fheight = fm.getHeight();
int rowHeight = lineCount * fheight;

theTable.setRowHeight(row, rowHeight);

Height of a row in a JTable component is based on an assigned value rather than on the preferred height of the renderers used in that row : JTable组件中一行的高度基于指定的值而不是该行中使用的渲染器的首选高度:

www.exampledepot.com/egs/javax.swing.table/RowHeight.html www.exampledepot.com/egs/javax.swing.table/RowHeight.html

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

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