getColumnClass() oddity
For a JTable on which some of column types are statically fixed and others are dynamically
resolved, if I write the return values from model.getColumnClass() as static ones, exception
is thrown:
Exception in thread"AWT-EventQueue-0" java.lang.IllegalArgumentException: Cannot format given Object as a Number
at java.text.DecimalFormat.format(DecimalFormat.java:487)
at java.text.Format.format(Format.java:140)
at javax.swing.JTable$DoubleRenderer.setValue(JTable.java:5172)
--snip--
Below is an example code.
I wonder is it a feature or a bug ...
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/* save and compile as TableDemoBad.java */
/* use -source 1.4 option for JDK 1.5 or above */
publicclass TableDemoBadextends JFrame{
privateboolean DEBUG =true;
Vector colNameVec;
Vector dataVec;
DefaultTableModel myModel;
public TableDemoBad(){
super("TableDemoBad");
setDefaultCloseOperation(EXIT_ON_CLOSE);
colNameVec =new Vector();
colNameVec.add("Column0");
colNameVec.add("Column1");
colNameVec.add("Column2");
colNameVec.add("IntColumn");
colNameVec.add("FloatColumn");
dataVec =new Vector();
myModel =new DefaultTableModel(dataVec, colNameVec){
public Class getColumnClass(int columnIndex){
Class retval =null;
switch (columnIndex){
case 0 : retval = Object.class;
case 1 : retval = Object.class;
case 2 : retval = Object.class;
case 3 : retval = Integer.class;
case 4 : retval = Float.class;
}
/* this works fine
retval = getValueAt(0, columnIndex).getClass();
*/
return retval;
}
};
JTable table =new JTable(myModel);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
JScrollPane scrollPane =new JScrollPane(table);
getContentPane().add(scrollPane, BorderLayout.CENTER);
addExampleRow();
}
void addExampleRow(){
Vector rv =new Vector();
rv.add("fofoo");
rv.add("babar");
rv.add("dederew");
rv.add(new Integer(18678));
rv.add(new Float(0.0029f));
myModel.addRow(rv);
}
publicstaticvoid main(String[] args){
TableDemoBad frame =new TableDemoBad();
frame.pack();
frame.setVisible(true);
}
}

