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

Vin*_*dre 3 java swing jtable

我有一个可编辑的jtable.当用户使用文本写入单元格时,如果输入的文本更符合单元格的大小,则表格中行的高度必须增加以适合用户输入的新文本.你能告诉我如何根据输入的文本行增加行的高度.

任何人都可以帮助我吗?我试图将JTextArea添加到一行,但这不起作用.

a_h*_*ame 7

您需要计算新输入文本所需的高度.根据您用于单元格的渲染器,您可以使用例如JTextArea.getLineCount()查找文本包含的行数.

然后,您需要检索渲染器使用的字体高度,并计算出所需的高度.

获得该数字后,可以使用JTable.setRowHeight(int, int)更改特定行的高度.

像这样的东西:

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);
Run Code Online (Sandbox Code Playgroud)