Author: Zafir Anjum
This article should actually be in the beginner section but for a bug in the swing code (JDK 1.2). Normally the code for this would be like this
// Do not use this - it won't work
JTable table = new JTable( 15, 3 );
JScrollPane sp = new JScrollPane( table );
sp.setColumnHeader(null); // setColumnHeader() has the bug
Since the above will not work, here's what you can do.
JTable table = new JTable( 15, 3 )
{
protected void configureEnclosingScrollPane() {
Container p = getParent();
if (p instanceof JViewport) {
Container gp = p.getParent();
if (gp instanceof JScrollPane) {
JScrollPane scrollPane = (JScrollPane)gp;
// Make certain we are the viewPort's view and not, for
// example, the rowHeaderView of the scrollPane -
// an implementor of fixed columns might do this.
JViewport viewport = scrollPane.getViewport();
if (viewport == null || viewport.getView() != this) {
return;
}
// scrollPane.setColumnHeaderView(getTableHeader());
scrollPane.getViewport().setBackingStoreEnabled(true);
scrollPane.setBorder(UIManager.getBorder("Table.scrollPaneBorder"));
}
}
}
};
What we've done above is basically stop the table header from getting added in the first place. Other than the commented line, the implementation of configureEnclosingScrollPane() is exactly the same as in JTable.
Posted On: 17-Jan-1999