• Post Reply Bookmark Topic Watch Topic
  • New Topic

Auto scroll a JTable

 
Robert Hill
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have searched around for a solution to this, but all I have found are solutions from 2002 and the methods they called to achieve this seem to have been removed.

I have the JTable and it fills up like it should, it scrolls correctly, but how do I get it to scroll automatically when a new row is added?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
after you add the new row

table.scrollRectToVisible(table.getCellRect(table.getRowCount()-1, 0, true));
[ February 28, 2008: Message edited by: Michael Dunn ]
 
Robert Hill
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.

The only problem was that it would not autoscroll to the last row, but to the last row -1. Oddly, just passing the actual row count didn't fix the issue but this did:


table.scrollRectToVisible(table.getCellRect(table.getRowCount()-1, table.getColumnCount(), true));

Thanks again!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try the code from my earlier post,
but wrap it in a SwingUtilities.invokeLater

this should give enough time for getRowCount() to update
 
Gilsom Jacoby
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works for me. thanks a lot.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!