This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wierd Behavior:TableCellRenderer

 
Sam Zheng
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);

getContentPane().add(scrollPane);
setBounds(100, 100, 300, 400);
//setVisible(true);
}

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

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)
setText(formatSci.format(((Double)value).doubleValue()));
else
setText(formatDbl.format(((Double)value).doubleValue()));
}
return this;
}
}
}
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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("");
 
Sam Zheng
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jose,
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
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic