[英]How to increase the height of a row in jtable depending on the text content entered in its cell
我有一個可編輯的jtable。 當用戶使用文本寫入單元格時,如果輸入的文本更符合單元格的大小,則表格中行的高度必須增加以適合用戶輸入的新文本。 你能告訴我如何根據輸入的文本行增加行的高度。
任何人都可以幫助我嗎? 我試圖將JTextArea添加到一行,但這不起作用。
您需要計算新輸入文本所需的高度。 根據您用於單元格的渲染器,您可以使用例如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);
JTable組件中一行的高度基於指定的值而不是該行中使用的渲染器的首選高度:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.