I've got a small test program set up to try and resolve this problem but I'm at a loss now. I have a jTable that has two columns - each of type double. The table uses a cell editor that selects all the contents on edit. (it also disables the editing on a mouse click to prevent masking of the problem) CODEpublic class DblCellEditor extends AbstractCellEditor implements TableCellEditor { JComponent component = new JTextField(); // This method is called when editing is completed. // It must return the new value to be stored in the cell. public Object getCellEditorValue() { return ((JTextField)component).getText(); }
// This method is called when a cell value is edited by the user. public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { // Configure the component with the specified value ((JTextField)component).setText(String.valueOf(value)); ((JTextField)component).selectAll(); // Return the configured component System.out.println("I've been Called!!"); return component; }
@Override public boolean isCellEditable(EventObject evt) { if (evt instanceof MouseEvent) { return false; } else { return true; } }
} The cells are initialised with 0, which when editing starts shows as 0.0 The first time you edit a selected cell (say by simply typing a number) the number you have typed overwrite the current cell value, so say we highlighted the cell and type 5 then <enter> the cell now has a value of 5. All good so far. Now if we go back and edit the cell again by reselecting it and typing 9 the new cell value is 5.09. The CellEditor's getTableCellEditorComponent was NOT called the second time we edit the cell. I've added a println statement to prove this. As I was typing that I had a relevation. The cell editor returns the value as a string so next time we edit the cell the default editor is called. So my question is now a tip - remember to return the correct type... CODEpublic Object getCellEditorValue() { String foo = ((JTextField)component).getText(); // you will need to check this doesn't throw an // exception if foo isn't a valid double. double i_spent_hours_on_this = Double.valueOf(foo); return new Double(i_spent_hours_on_this); } |
|