Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_java thread: Custom Table Header Cell Renderer


Message #1 by "David Vinas" <dvinas@m...> on Fri, 9 Nov 2001 21:04:59
Gotta love it when you answer your own questions.  I got this to work, 
finally.  Turns out, the DefaultTableCellRenderer, which I copied to do 
this enhancement, overrides the validate(), revalidate(), repaint(), and 
firePropertyChange() methods with no-ops, in order to increase 
performance.  

eg.: public void validate() {}

This is mentioned in the class documentation for 
DefaultTableCellRenderer.  It says you should definitely remember to do 
this when writing your own TableCellRenderer.  What isn't mentioned in the 
class documentation, but is mentioned in the source code, is the following:

"Great care should be taken when writing your own renderer to weigh the 
benefits and drawbacks of overriding methods like these."

Well, the overriding of the validate() method was causing my panel to 
display sans labels.  If I comment out this code, everything works as it 
should.  

-David

> I'm using JDK 1.3.0 and I need to create a custom cell-renderer for a 
> JTable.  I need my headers to display 2-column titles.  I've already 
> created a custom TableModel and JTable to take care of some of the other 
> functionality I needed, but I'm having problems with the header renderer.
> 
> I decided to derive my renderer from JPanel, and add two JLabels in a 
> Border layout (NORTH & SOUTH).  As far as I can tell, this would be easy 
> to do for a normal table cell.  I would just register my renderer as a 
> default for the my custom header class, which contains the two strings.  
> However, check out this method from JTable:
> 
>     public void addColumn(TableColumn aColumn) {
>         int modelColumn = aColumn.getModelIndex();
> -->     String columnName = getModel().getColumnName(modelColumn);
>         if (aColumn.getHeaderValue() == null) {
>             aColumn.setHeaderValue(columnName);
>         }
>         getColumnModel().addColumn(aColumn);
>     }
> 
> As you can see, this is hard-coded to assume a String name, as the 
default 
> renderer is derived from JLabel.  So, I also overrode this method, in 
> order to get the the correct object from my TableModel.  
> 
> At any rate, after 2 days, and numerous similar customizations, I still 
> can't get this to display.  I get a blank panel, but no labels.  I keep 
> thinking there must be an easier way.  Has anyone done this before?
> 
> -David

  Return to Index