Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Refreshing JTable after adding a row

843806Nov 19 2008 — edited Nov 19 2008
Hello all,

I have a JTable, and I would like to refresh its display each time I add a new row.
I am currently using the following code, but as the table size is growing, it becomes slower and slower.
    {
    tableModel.addRow(newRow);

    //scroll viewport to last added row
    scrollBottomTable(tableLog);

    }

    private void scrollBottomTable(JTable scrollTable) {
        int index = scrollTable.getRowCount() - 1;

        if (index > 0) {
            //Scroll automatically the viewport to the last row added
            JViewport viewport = (JViewport) scrollTable.getParent();

            java.awt.Rectangle rect = scrollTable.getCellRect(index, 0, true);

            java.awt.Point pos = viewport.getViewPosition();

            rect.translate(-pos.x, -pos.y);

            viewport.scrollRectToVisible(rect);

            //Update Graphics
            scrollTable.update(scrollTable.getGraphics());
        }
    }
Does anyone has a most improved way to update graphically my table as I add rows ?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 17 2008
Added on Nov 19 2008
7 comments
1,272 views