Java Swing:表格单元渲染器创建无限循环

Java Swing:表格单元渲染器创建无限循环,java,swing,jtable,rendering,tablecellrenderer,Java,Swing,Jtable,Rendering,Tablecellrenderer,我想创建一个带有单元格换行的swing表,因此我创建了以下使用textarea的渲染器。 问题是,如果我在渲染器中使用setRowHeight方法设置行高,它会创建一个无限循环。我想知道是否有一种方法可以实现这一点(无无限循环的细胞系包裹) 我已经创建了一个方法来根据渲染器的当前行高更新行高。但要使用它,您需要为渲染器设置正确的高度(可能使用setPreferredSize()方法)。当您的表内容是最新的时,只需调用此方法 public static void updateRowHeight(J

我想创建一个带有单元格换行的swing表,因此我创建了以下使用textarea的渲染器。 问题是,如果我在渲染器中使用
setRowHeight
方法设置行高,它会创建一个无限循环。我想知道是否有一种方法可以实现这一点(无无限循环的细胞系包裹)


我已经创建了一个方法来根据渲染器的当前行高更新行高。但要使用它,您需要为渲染器设置正确的高度(可能使用setPreferredSize()方法)。当您的表内容是最新的时,只需调用此方法

public static void updateRowHeight(JTable table) {
    final int rowCount = table.getRowCount();
    final int colCount = table.getColumnCount();
    for (int i = 0; i < rowCount; i++) {
        int maxHeight = 0;
        for (int j = 0; j < colCount; j++) {
            final TableCellRenderer renderer = table.getCellRenderer(i, j);
            maxHeight = Math.max(maxHeight, table.prepareRenderer(renderer, i, j).getPreferredSize().height);
        }
        table.setRowHeight(i, maxHeight);
    }
}
publicstaticvoidupdaterowheight(JTable表){
final int rowCount=table.getRowCount();
final int colCount=table.getColumnCount();
对于(int i=0;i
您显然没有注意到标签上大写的消息,该消息表示“请勿使用此标签;”。请阅读标签后再贴在帖子上。
public static void updateRowHeight(JTable table) {
    final int rowCount = table.getRowCount();
    final int colCount = table.getColumnCount();
    for (int i = 0; i < rowCount; i++) {
        int maxHeight = 0;
        for (int j = 0; j < colCount; j++) {
            final TableCellRenderer renderer = table.getCellRenderer(i, j);
            maxHeight = Math.max(maxHeight, table.prepareRenderer(renderer, i, j).getPreferredSize().height);
        }
        table.setRowHeight(i, maxHeight);
    }
}