Swing renderer to display the complete text in a column creates infinite loop

老子叫甜甜 提交于 2019-12-20 05:42:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!