0
votes

Basically: my JTable wants special row-heights for each row. Each renderer used writes its requested row-height for each cell to a map in the table, the table has a method updateRowHeights which uses the map to set the row-heights. The problem is that the map with row-heights is only filled with heights by the renderer. Hence, the first time the table is rendered the map is still empty. Therefore, when the table is displayed the first time the row-heights are not set.

As a not-so-pretty workaround I have added a component listener to my table which updates the row-heights. This works fine most of the time, but for example it is not called when the table lives in a scrollpane inside a tabbedpane. Preferably I would have a method onRenderingFinished or something like that, in which I can call the row-heights method.

I am doing something like in the first answer of How to set the RowHeight dynamically in a JTable. There it is mentioned that you should not change the rowheights of your table from within the renderer (I agree with that) and that "you set it (call updateRowHeight) once after the initialization is completed and whenever any of the state it depends on is changed."

So my question is: where/when do I call updateRowHeight?

Is there an event that tells me the renderer is finished?

1

1 Answers

2
votes

If you only want to update table row height depended on the highest cell height, you only need to call this method after the table content is set.

public static void updateRowHeight(JTable table, int margin) {
    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 + margin);
    }
}

Margin is optional parameter to provide additional space for each row (may be 0 or positive).

Please note: this method can provoke performance problems when table is very large and/or contains lots of HTML strings.