问题
I want to render a swing table adjusting the row's height, depending on how much text is displayed in a column so that all the text is shown in the cell.
The problem is that when I set the height to the row an infinite loop is created. How could I solve it?
This is my renderer
final class RenderTextAreaMessage extends DefaultTableCellRenderer {
JTextArea textareaMessage;
@Override
public Component getTableCellRendererComponent(JTable aTable, Object aNumberValue, boolean aIsSelected,
boolean aHasFocus, int aRow, int aColumn ) {
System.out.println("aa");
String value = (String)aNumberValue;
textareaMessage = new JTextArea();
textareaMessage.setLineWrap(true);
textareaMessage.setWrapStyleWord(true);
textareaMessage.setText(value);
textareaMessage.setBorder(null);
textareaMessage.setMargin(null);
Component renderer = super.getTableCellRendererComponent(
aTable, aNumberValue, aIsSelected, aHasFocus, aRow, aColumn
);
Font fontType = textareaMessage.getFont();
FontMetrics fontMet = textareaMessage.getFontMetrics(fontType);
int fheight = fontMet.getHeight();
int lineCount = textareaMessage.getLineCount();
int rowHeight = lineCount * fheight;
aTable.setRowHeight(aRow,rowHeight+6);
return textareaMessage;
}
}
来源:https://stackoverflow.com/questions/37700309/swing-renderer-to-display-the-complete-text-in-a-column-creates-infinite-loop