It's not a secret anymore!*
The moose likes Swing / AWT / SWT and the fly likes Wierd Behavior:TableCellRenderer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Wierd Behavior:TableCellRenderer" Watch "Wierd Behavior:TableCellRenderer" New topic

Wierd Behavior:TableCellRenderer

Sam Zheng
Ranch Hand

Joined: Nov 29, 2000
Posts: 61
I have a TableCellRenderer problem that seems to be very wierd. I seached this forum and couldn't find any discussion about this behavior. Please have a look and let me know if you have any solution.
In the following code, all the table cell contains Double.class object. It has three columns. In the second column (column = 1), I want the double to be displayed in scientific format. For example, 12345 as 1.234E4. The problem I have is that once I enter one number in any of the table, the number is displayed all over the table! Say I enter 100 at cell (0,1),
after I hit enter. The number is correctly displayed as 1E2. Then I click anywhen within the table, this number (1E2) is propagated all over the table! That is crazy! Here is the complete code:

import javax.swing.*;
import java.awt.*;
import java.text.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
public class Test extends JFrame {
JTable table;
Object data[][];
String[] columnNames = {"first", "second", "third"};
MyTableModel tableModel;

/** Creates new Test */
public Test() {
data = new Object[100][3];
table = new JTable(new MyTableModel(data, columnNames));
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setDefaultRenderer(Double.class, new ScientificDoubleRenderer());
JScrollPane scrollPane = new JScrollPane(table);

setBounds(100, 100, 300, 400);

public static void main(String argv[]){
Test t = new Test();

class MyTableModel extends DefaultTableModel{
String column[];
Object data[][];

public MyTableModel(Object dat[][], String[] col){
super(dat, col);
column = col;
data = dat;

public Class getColumnClass(int c){
return Double.class;

class ScientificDoubleRenderer extends JLabel implements TableCellRenderer{
DecimalFormat formatSci;
DecimalFormat formatDbl;
String patternSci = "0.###E0";
String patternDbl = "#######.###";
public ScientificDoubleRenderer(){
formatSci = new DecimalFormat(patternSci);
formatDbl = new DecimalFormat(patternDbl);
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column){
if (value != null) {
if(column == 1)
return this;
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Once the value of the JLabel has been set to the one you introduced, that value is used for all the cells because there is missing an statement like
if(value==null) setText("");

SCJP2. Please Indent your code using UBB Code
Sam Zheng
Ranch Hand

Joined: Nov 29, 2000
Posts: 61
Thanks! Adding the line
if(value==null) setText("");

does fix the problem. I am curious to know why the original code produces the wierd behavior, why the value I entered is displayed all over the cells.
Would you shed some light?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
All the cells in the table are rendered by the same JLabel. When value is null --before entering a data in the cell-- we are returning the JLabel whose text was set by the last cell edition (Because there were no a sentence like if (value==null) setText("");). In this way the value entered is replicated when rendering all the cells.
It is sorta covered in the JavaRanch Style Guide.
subject: Wierd Behavior:TableCellRenderer
Similar Threads
Help: JTable with data validation
? JTable getColumnName
Help updating cells in a JTable
JScrollPane within JTable cell issue