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

