Thanks John. That worked (though I wish Sun had something on the JTable
as a cleaner alternative)
> Thanks John. That worked (though I wish Sun had something on the JTable
> as a cleaner alternative)
What did you have in mind? I cannot think of much they could do to make
it cleaner. Making the header a separate component seems appropriate,
since the header normally is fixed while the main part of the table
scrolls. They could have created a separate table implementation that
contains a table and a header, and forwards method calls to the
appropriate child component, but that seems pretty ugly, and it introduces
a lot of cruft.
I suppose they could have provided an additional component that would deal
with placing the header, but that is really simple to create:
public class TablePanel extends JPanel {
public TablePanel( JTable table ) {
super( new BorderLayout() );
add( table, BorderLayout.CENTER );
add( table.getTableHeader(), BorderLayout.NORTH );
}
}
With the above component, instead of adding the JTable to your UI, you can
add new TablePanel(table) instead, and it will show the header above the
table.
I know that many people are surprised when they place a JTable outside of
a JScrollPane and the table header is not shown. This behavior is well
documented at the top of the JTable Javadocs, but there are some people
who just jump in without carefully reading the docs (who?, ME? never!).
So I guess what it comes down to is that the relationship between the
table and its header is not at all complicated, but it is not intuitive
either. While it certainly would be a good thing for it to be intuitive,
making it so could make the code more complex and overshadow the benefits.
I cannot think of a simple way to make it more intuitive.

Signature
Regards,
John McGrath
ian.a.desouza@gmail.com - 11 Jan 2005 21:11 GMT
Hi John,
I just wanted to group attributes of some particular entity. Each set
of attributes in a different table with all the rows showing. All the
tables in a vertical Box. Then have the enveloping Box in a
JScrollPane. When I did this I was surprized by the column headers not
showing (since I had defined the getColumnName in the table model --
that's my issue, since it doesn't mention that in the docs of
getColunmName of TableModel).
Anyway, I can understand if it causes headaches, since one would not
usually want the header to scroll. I like your idea of writing a
wrapper class that does the scrolling and has a method like
addTable(JTable table), that would extract the header, add it to the
box (which would be in the JScrollPane), then add the table itself; all
with the appropriate layout manangers.
Again, thanks for your help.