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);

}

}

[4484 byte] By [hiwaa] at [2007-10-2 17:12:26]
# 1

It would be a feature of your code. It should be:

switch (columnIndex){

case 0 : retval = Object.class; break;

case 1 : retval = Object.class; break;

case 2 : retval = Object.class; break;

case 3 : retval = Integer.class; break;

case 4 : retval = Float.class; break;

}

return retval;

or maybe

switch (columnIndex)

{

case 3 : return Integer.class;

case 4 : return Float.class;

default: return Object.class;

}

camickra at 2007-7-13 18:27:42 > top of Java-index,Desktop,Core GUI APIs...
# 2

> It would be a feature of your code. It should be:

> > switch (columnIndex){

>case 0 : retval = Object.class; break;

>case 1 : retval = Object.class; break;

>case 2 : retval = Object.class; break;

>case 3 : retval = Integer.class; break;

>case 4 : retval = Float.class; break;

> }

> return retval;

>

>

> or maybe

> > switch (columnIndex)

> {

>case 3 : return Integer.class;

>case 4 : return Float.class;

>default: return Object.class;

> }

>

Oh, what a beginneresque error I have done!

I'm so ashamed of.

At first it was:

switch (columnIndex){

case 0 : return Object.class;

case 1 : return Object.class;

case 2 : return Object.class;

case 3 : return Integer.class;

case 4 : return Float.class;

}

Then, after getting 'no return value' error from compiler, I too hastily rewrote the case parts

thereby ignoring break statements.

Again, camickr is a great savior. Thanks a lot!

(I eagerly hope that javac compiler rectify the 'no return value' error emitting behavior.)

hiwaa at 2007-7-13 18:27:42 > top of Java-index,Desktop,Core GUI APIs...