Author: Zafir Anjum
Make a cell or row visible programmatically.
static void setCellVisible( JTable table, int row, int col )
{
Container p = table.getParent();
if (p instanceof JViewport) {
Container gp = p.getParent();
if (gp instanceof JScrollPane) {
JScrollPane scrollPane = (JScrollPane)gp;
// Make sure the table is the main viewport
JViewport viewport = scrollPane.getViewport();
if (viewport == null || viewport.getView() != table) {
return;
}
Rectangle cellrect = table.getCellRect( row, col, true );
Rectangle viewrect = viewport.getViewRect();
if( viewrect.contains( cellrect ) )
return;
Rectangle union = viewrect.union( cellrect );
int x = (int)(union.getX() + union.getWidth() - viewrect.getWidth());
int y = (int)(union.getY() + union.getHeight() - viewrect.getHeight());
viewport.setViewPosition( new Point( x, y ) );
}
}
}
Posted On: 7-Jan-1999