When I try to assign a custom renderer to a JTable using
setDefaultCellRenderer is never called.
Strangely enough, if I assign the same renderer using
JTable.getColumn().setCellRenderer, it does get called!
Is there anything I missed?
My code:
/*
* Created on 21-feb-2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package voorth.lab;
import java.util.Date;
import javax.swing.*;
/**
* @author Voorthuijsen
*
* This class demonstrates a problem with JTable.setDefaultCellRenderer();
*/
public class DefaultRendererExample extends JFrame
{
private javax.swing.JPanel jContentPane = null;
private JTable jTable = null;
private JScrollPane jScrollPane = null;
private RendererTestModel rendererTestModel = null; //
@jve:decl-index=0:
private JTable getJTable() {
if (jTable == null) {
jTable = new JTable();
jTable.setModel(getRendererTestModel()); // Generated
jTable.setDefaultRenderer(Date.class, new DateCellRenderer()); // This
doesn't work
// String colName = jTable.getColumnName(0);
// jTable.getColumn(colName).setCellRenderer(new DateCellRenderer()); //
but this does!
}
return jTable;
}
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setViewportView(getJTable()); // Generated
}
return jScrollPane;
}
private RendererTestModel getRendererTestModel() {
if (rendererTestModel == null) {
rendererTestModel = new RendererTestModel();
}
return rendererTestModel;
}
public static void main(String[] args)
{
new DefaultRendererExample().show();
}
public DefaultRendererExample() {
super();
initialize();
}
private void initialize() {
this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); //
Generated
this.setSize(300,200);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
private javax.swing.JPanel getJContentPane() {
if(jContentPane == null) {
jContentPane = new javax.swing.JPanel();
jContentPane.setLayout(new java.awt.BorderLayout());
jContentPane.add(getJScrollPane(), java.awt.BorderLayout.CENTER); //
Generated
}
return jContentPane;
}
}
/*
* Created on 21-feb-2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package voorth.lab;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.table.AbstractTableModel;
/**
* @author Voorthuijsen
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class RendererTestModel extends AbstractTableModel
{
/* (non-Javadoc)
* @see javax.swing.table.TableModel#getColumnCount()
*/
public int getColumnCount()
{
return 2;
}
/* (non-Javadoc)
* @see javax.swing.table.TableModel#getRowCount()
*/
public int getRowCount()
{
return 3;
}
/* (non-Javadoc)
* @see javax.swing.table.TableModel#getValueAt(int, int)
*/
public Object getValueAt(int rowIndex, int columnIndex)
{
if (columnIndex == 0) return new GregorianCalendar(2005,
Calendar.FEBRUARY, 1+ rowIndex).getTime();
if (columnIndex == 1) return "line " + (1 + rowIndex);
return null;
}
}
/*
* Created on 21-feb-2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package voorth.lab;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.table.DefaultTableCellRenderer;
/**
* @author Voorthuijsen
*/
public class DateCellRenderer extends DefaultTableCellRenderer
{
protected void setValue(Object value)
{
if (value instanceof Date) setText(new
SimpleDateFormat("yyyy-MM-dd").format((Date)value));
else super.setValue(value);
}
}
Paul van Rossem - 21 Feb 2005 16:17 GMT
> When I try to assign a custom renderer to a JTable using
> setDefaultCellRenderer is never called.
[quoted text clipped - 162 lines]
> }
> }
In your TableModel you must also override getColumnClass()
so that the JTable knows for which columns it must use your
DateCellRenderer.
Paul.
Henk van Voorthuijsen - 21 Feb 2005 16:25 GMT
> > Is there anything I missed?
> >
> In your TableModel you must also override getColumnClass()
> so that the JTable knows for which columns it must use your
> DateCellRenderer.
> Paul.
Curious. I could have sworn it didn't work the last time I tried that. Tried
your suggestion right away and it _did_ work. Thanks!
Henk
Henk van Voorthuijsen - 22 Feb 2005 12:26 GMT
> In your TableModel you must also override getColumnClass()
> so that the JTable knows for which columns it must use your
> DateCellRenderer.
> Paul.
OK, that did work in the sample. Unfortunately, getColumnClass() seems to be
necessary, but not sufficient. In my production code the same approach
fails. I must still be missing something. Are there any JTable properties
that need to be set as well?
Roland - 22 Feb 2005 17:50 GMT
>>In your TableModel you must also override getColumnClass()
>>so that the JTable knows for which columns it must use your
[quoted text clipped - 5 lines]
> fails. I must still be missing something. Are there any JTable properties
> that need to be set as well?
One possibility (and probably the easiest):
yourTable.setDefaultRenderer(YourClass.class, yourTableCellRenderer);

Signature
Regards,
Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \