I'm trying to automatically scroll to the bottom of a JTable (inside a scrollPane) that has variable row height and is getting refreshed periodically. Is there a listener I can override, or an event I can listen to, that will happen after the table has been redrawn with the new rows at the bottom?
I implemented variable height by overriding the renderer for a column to reset the row height if the text in the column is longer than can be displayed in one line at the column's width.
private void adjustRowHeight( )
{
if ( _table == null )
{
return;
}
int cWidth = _table.getTableHeader().getColumnModel().getColumn(_column).getWidth();
_textComponent.setSize( new Dimension( cWidth - 2*HORIZONTAL_GAP, MAX_HEIGHT ) );
int prefH = _textComponent.getPreferredSize().height;
boolean isTooBig = prefH > MAX_HEIGHT;
if (isTooBig)
{
prefH = MAX_HEIGHT;
}
_table.setRowHeight(_row, prefH + 2 );
}
My scrollToBottom
code, is below.
Before I made the rows variable height this referenced the row count instead of MAX_VALUE, but referencing a valid row index uses the default row height to calculate the row's location, instead of variable row height.
_table.scrollRectToVisible( _table.getCellRect( Integer.MAX_VALUE, 0, true ) );
Using Integer.MAX_VALUE overrides the default calculation to use the table's height as the y value for the rectangle. What i think is happening is that this code is getting called before the table is redrawn (and the height reset), even though the new rows have been added to the table. Is there an event I can listen to to know when the height has been reset on the table, so I can then scrollToBottom? Or is there something else I'm missing here?