How to scroll to a cell in a JTable



I have a JTable embedded in a JScrollPane.

Through code, I want to select a specific cell in the table and make sure it scrolls into view.

Here is my code (note: my JTable only has one row, but a large number of columns):

int index = 5; // column that I want to show

tblDiscContent.getColumnModel().getSelectionModel().
    setSelectionInterval(index, index);

scrollDiscContent.getViewport().scrollRectToVisible(
    tblDiscContent.getCellRect(
        0,tblDiscContent.convertColumnIndexToView(index), false));

The last statement should scroll the cell into view, but it doesn't. It scrolls too far to the right, leaving my cell hidden to the left. The amount of error does is not constant and depends on which cell I try to select.

--
Josef Garvi

"Reversing desertification through drought tolerant trees"
http://www.eden-foundation.org/

new income - better environment - more food - less poverty
.