You guys were so awesome in point me in the right direction on my last question and I have sort of an extension of my original question here:
How to set a JTable column as String and sort as Double?
As I now have my price column formatted as $###,##0.00 by using my custom cell renderer, I have now set up a JTextField Editor for the cell as well. The editing of the cell works just fine except for when the value is updated, the number format set in my custom renderer no longer seems to format the cell (I'm loosing the $ after edit is committed). Is this renderer not supposed to render the cells even after the initial display of the data?
I have tried to use the following with no luck:
((AbstractTableModel) table.getModel()).fireTableDataChanged();
I was hoping that this would force the table to revalidate and repaint the cells using the custom renderer to render the new values, but this unfortunately did not work...
Am I missing something... Obviously, but what?
When your editor concludes, the table's
editingStopped()
method collects the new value viagetCellEditorValue()
and uses it tosetValueAt()
in the model. The model, in turn, shouldfireTableCellUpdated()
, which will invoke the prescribed renderer. Extending the default should be enough to handleNumber
formatting. In other cases, it may be convenient to use an instance of your renderer as your editor component; this example shows a typical implementation.Addendum: Here's a basic example using the default editor and renderer implementations.
Addendum: Thanks to helpful comments from @mKorbel, I've updated the example to select the cell's text for editing, as described in @camickr's article Table Select All Editor.