Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2005

Tip: Looking for answers? Try searching our database.

JTable.setDefaultCellRenderer has no effect

Thread view: 
Henk van Voorthuijsen - 21 Feb 2005 16:00 GMT
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_/ /__/
/  \ /_/ /  \



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.