Autoscrolling JTables

by Kwong

Had to deal with Java Swing for a school project and faced an issue with getting a JTable to autoscroll after adding an entry during runtime. After more than 3 hours of scouring the internets to no avail.. i put together a dirty hack that sorta works (well enough for me, I guess)

private synchronized void autoScroll() {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            eventTable.scrollRectToVisible(eventTable.getCellRect(
                 eventTable.getRowCount()-1, eventTable.getColumnCount(), true));
        }
    });
}

where eventTable is the JTable constantly being updated at runtime. Basically call autoScroll() each time an update is done. Pretty sure there’s a better way to do this but what the hell, hope this helps other people who’s having a hard time with Swing/JTables.

Cheers.