How do I programatically scroll to the selected row in a JTable or JTree?

Sandip Chitale

Basically you need to get the rectangle of the row and then use -

public void scrollRectToVisible(Rectangle aRect)
Forwards the scrollRectToVisible() message
to the JComponent's parent. Components that
can service the request, such as a
Viewport, override this method and perform
the scrolling.

To get the rectangle -

For JTable use -

public Rectangle getCellRect(int row,
                             int column,
                             boolean includeSpacing)
Returns a rectangle locating the cell that
lies at the intersection of row and column.
If includeSpacing is true then the value
returned includes the intercellSpacing 
margin. If it is false, then the returned
rect is inset by half of intercellSpacing.
(This is the true frame of the cell)

// set column to 0

For JTree use -

public Rectangle getRowBounds(int row)
Returns the Rectangle that the node at the specified row is drawn in.

0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact