0

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?

rediVider
  • 1,266
  • 2
  • 13
  • 30
  • 1
    _implemented variable height by overriding the renderer for a column to reset the row height_ - depending on _when_ the renderer does so, it might be very wrong: it *must not* doing it in getTableCellRendererComponent – kleopatra Oct 13 '12 at 10:10

2 Answers2

2

there are two ways,

  • moving with JScrollBar

  • moving with JViewport,

I'd be prefering this way too, then there is possible to manage with this Point and its coordinates, for example

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?

Is there an event I can listen to to know when the height has been reset on the table, so I can then scrollToBottom?

private void addTableListener() {
    model.addTableModelListener(new TableModelListener() {

        @Override
        public void tableChanged(TableModelEvent tme) {
            if (table.getRowCount() > 0) {
                if (tme.getType() == TableModelEvent.INSERT) {
                   
                } else if (tme.getType() == TableModelEvent.DELETE) {
                    
                } else if (tme.getType() == TableModelEvent.UPDATE) {
                   
                }
            }
        }
    });
}

everything depends od ListSelectionModel,

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • This does not answer the question. scrollRectToVisible is already occurring, and I neither need nor want to change the user's selection. The problem is that scrollRectToVisible is happening before the correct rectangle y coordinate is calculated. – rediVider Oct 12 '12 at 17:54
  • ??? this issue I can't get from your code???, no idea, my linked code have got the same issue get/set rows coordinates on the fly, better could be post your onw issues translated to the [SSCCE](http://sscce.org/m) , to avoids any possible lacks came from rest of your code, sorry battery not included from your code posted here – mKorbel Oct 12 '12 at 18:07
  • Thx for the edit. The tableChanged method is also called before my renderer resizes the row height, so i run into the same problem. – rediVider Oct 16 '12 at 18:40
0

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?

Is there an event I can listen to to know when the height has been reset on the table, so I can then scrollToBottom?

Yes. Found in a parent class of JTable, Component#addComponentListener( ComponentListener ) where I can override the componentResized method.

rediVider
  • 1,266
  • 2
  • 13
  • 30
  • 1
    not entirely sure if that's what you really want: the event is fired for every size change. If f.i. the user changes the width while looking at the top of the rows, s/he might be quite annoyed when scrolled to the bottom. – kleopatra Oct 13 '12 at 10:16
  • Thanks. Good point. The user has an option to 'Auto-Scroll'. I was thinking that would cover it, but I should probably check to see if the row count has changed as well, before scrolling to the bottom. – rediVider Oct 15 '12 at 18:01