Why is object reference showing in JTable when editing cell?
I'm using a custom CellRenderer, see code below. I have a feeling something I'm doing in this is causing the problem. The CellRenderer itself seems to work fine showing the actual value, but when I edit the cell, I see an object reference instead of the actual value.
Here's the code:
Code :
public class PlannerCellRenderer extends JLabel implements TableCellRenderer, Serializable {
private static final long serialVersionUID = 1L;
private Border unselectedBorder = null;
private Border selectedBorder = null;
boolean isBordered = true;
public PlannerCellRenderer(boolean isBordered) {
super();
this.isBordered = isBordered;
setOpaque(true); // MUST do this for background to show up.
}
public Component getTableCellRendererComponent(JTable table, Object cell, boolean isSelected, boolean hasFocus, int row, int column) {
Color newColor;
if(cell == null) {
super.setText("");
if (isSelected) {
super.setBackground(new Color(50,50,100));
} else {
super.setBackground(new Color(255,255,255));
}
return this;
}
if(column == 0 ) {
super.setText((String)cell);
super.setBackground(new Color(255,255,255));
super.setForeground(new Color(0,0,0));
super.setHorizontalAlignment(JLabel.LEFT);
return this;
}
switch(((DisplayCell)cell).getCellType()) {
case DisplayCell.BLANK:
newColor = new Color(255,255,255); // White
break;
case DisplayCell.FIRSTTYPE:
newColor = new Color(128,128,128); // Grey
break;
case DisplayCell.SECONDTYPE:
newColor = new Color(0,255,0); // Green
break;
default:
newColor = new Color(255,255,255); // White
break;
}
if(((DisplayCell)cell).getNumberResources() == 0) {
super.setText("");
} else {
super.setText(Integer.toString(((DisplayCell)cell).getNumberResources()));
}
super.setHorizontalAlignment(JLabel.CENTER);
if (isSelected) {
super.setBackground(new Color(50, 50, 100));
} else {
super.setBackground(newColor);
}
setToolTipText(DisplayCell.CELLTYPE[((DisplayCell)cell).getCellType()]);
return this;
}
}
Re: Why is object reference showing in JTable when editing cell?
Could you elaborate what you mean?
Do you experience output similar to java.lang.Object@1ba34f2?
Re: Why is object reference showing in JTable when editing cell?
Exactly. But, I only see something similar to "java.lang.Object@1ba34f2" when I click on the cell to edit it. It displays fine otherwise.
Re: Why is object reference showing in JTable when editing cell?
OK, I found my own problem.
The toString() method is called on the object the CellRenderer is displaying. In my case, DisplayCell. I failed to override the toString() method for this, so the default was returning the reference string name.