Basic cell renderer question. Help a newbie please!

Hi,

I'm new to Java, and am trying to make a JTable with a custom cell renderer. Eventually, I want the renderer to format a number as a simple number if less than 1000 (i.e. 999.99 etc) but switch to scientific notation when bigger than this (i.e. 1e3 etc). I'm planning to do this with a simple if block in a custom cell renderer.

My problem at the moment is that I'm having trouble getting a renderer to work at all. This code is my go at a test with applying the renderer to one column of the table only. It produces a table, but then produces "Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String" when I try to edit anything.

Any help as to where I'm going wrong would be very greatfully appreciated, as I'm a bit lost!

Cheers,

Arwel

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.table.*;

import javax.swing.DefaultCellEditor;

import java.util.*;

import java.text.*;

import java.text.NumberFormat;

import java.text.DecimalFormat;

public class ShipTable {

public static class ShipTableModel extends AbstractTableModel {

private String[] headings = new String[] {

"Parameter", "Minimum", "Value", "Maximum","Std Error","Fit?"

};

private Object[][] data = new Object[][]{

{"Param 1", new Float(1), new Float(3e-12), new Float(5),new Float(0.1), Boolean.TRUE},

{"Param 1", new Float(1), new Float(3), new Float(5),new Float(0.1), Boolean.TRUE},

{"Param 1", new Float(1), new Float(3), new Float(5),new Float(0.1), Boolean.TRUE},

{"Param 1", new Float(1), new Float(3), new Float(5),new Float(0.1), Boolean.TRUE},

{"Param 1", new Float(1), new Float(3), new Float(5),new Float(0.1), Boolean.TRUE}

};

public int getRowCount() {return data.length;}

public int getColumnCount() {return data[0].length;}

public Object getValueAt(int row, int column) {

return data[row][column];

}

public String getColumnName(int column) {

return headings[column];

}

public Class getColumnClass(int column) {

switch (column) {

case 5:

return Boolean.class;

default:

return Object.class;

}

}

public void setValueAt(Object value, int row, int column) {

data[row][column] = value;

fireTableDataChanged();

};

public boolean isCellEditable(int row, int col) {

if (col == 0) {

return false;

} else {

return true;

}

}

}

public static void main(String[] args)

{

TableModel model = new ShipTableModel();

JTable table = new JTable(model);

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

table.setSize(200,200);

table.setRowSelectionAllowed(false);

int vColIndex = 3;

TableColumn col = table.getColumnModel().getColumn(vColIndex);

col.setCellRenderer(new MyTableCellRenderer());

JFrame frame = new JFrame("Hello World");

frame.getContentPane().add(new JScrollPane(table));

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(500,500);

frame.setVisible(true);

}

}

class MyTableCellRenderer extends JLabel implements TableCellRenderer {

Number numberValue;

NumberFormat nf;

DecimalFormat df = new DecimalFormat("#.####");

String _formattedValue;

public Component getTableCellRendererComponent(JTable table,

Object value, boolean isSelected, boolean hasFocus,

int row, int col) {

Float _value = (Float)value;

if (value == null) {

_formattedValue = "Not Set";

} else {

_formattedValue = df.format(value);

}

JLabel testLabel = new JLabel(_formattedValue);

return testLabel;

}

}

[3883 byte] By [Arwela] at [2007-10-3 2:22:18]
# 1
Well, obviously there's a wrong cast. Which line throws the exception?
CeciNEstPasUnProgrammeura at 2007-7-14 19:21:16 > top of Java-index,Java Essentials,New To Java...
# 2

Your setValue() method make sthe assumption that that 'value' is a Float but it is a String. You need something like

public void setValueAt(Object value, int row, int column)

{

data[row][column] = Float.valueOf((String)value);

fireTableDataChanged();

};

Note - you need to handle the posibility that the user does not enter a valid number.

P.S. You only need to fireTableCellUpdated(...)

Message was edited by:

sabre150

sabre150a at 2007-7-14 19:21:16 > top of Java-index,Java Essentials,New To Java...
# 3
> Note - you need to handle the posibility that the> user does not enter a valid number.It does! It handles it by terminating. That teaches the user to watch what he's typing! ;)
CeciNEstPasUnProgrammeura at 2007-7-14 19:21:16 > top of Java-index,Java Essentials,New To Java...
# 4

> > Note - you need to handle the posibility that the

> > user does not enter a valid number.

>

> It does! It handles it by terminating. That teaches

> the user to watch what he's typing! ;)

:-) Funny education system in France.

Teacher to Johnny : 1 + 1 = ?

Johnny : 7?

Teacher : Wrong dumbo - off with your head!

sabre150a at 2007-7-14 19:21:16 > top of Java-index,Java Essentials,New To Java...
# 5
> Teacher to Johnny : 1 + 1 = ?> Johnny : 7?> Teacher : Wrong dumbo - off with your head!And we still have 5 million more people than you do.
CeciNEstPasUnProgrammeura at 2007-7-14 19:21:16 > top of Java-index,Java Essentials,New To Java...
# 6

Hey, Thanks that works! :o)

However, it now falls over when I try to change the check boxes, which I assume because SetValueAt is expecting a string but getting Boolean.

Could I fix the problem by doing a conditional execution in SetValueAt to check what 'value' is before assigning? Or is there a better way...

But anyway, many thanks 4 the help.

Arwela at 2007-7-14 19:21:16 > top of Java-index,Java Essentials,New To Java...
# 7

> However, it now falls over when I try to change the

> check boxes, which I assume because SetValueAt is

> expecting a string but getting Boolean.

> Could I fix the problem by doing a conditional

> execution in SetValueAt to check what 'value' is

> before assigning? Or is there a better way...

>

That is how I do it! As the whim takes me I use 'instanceof' or the column number.

sabre150a at 2007-7-14 19:21:16 > top of Java-index,Java Essentials,New To Java...
# 8
> That is how I do it! As the whim takes me I use> 'instanceof' or the column number.IMHO the column number would be the cleaner implementation ...
JoachimSauera at 2007-7-14 19:21:16 > top of Java-index,Java Essentials,New To Java...