Swing渲染器在列中显示完整文本会创建无限循环

时间:2016-06-08 10:45:48

标签: java swing jtable tablecellrenderer

我想渲染一个调整行高度的swing表,具体取决于列中显示的文本数量,以便所有文本显示在单元格中。

问题在于,当我将高度设置为行时,会创建一个无限循环。我怎么能解决它?

这是我的渲染器

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;
  }
}

0 个答案:

没有答案