Let's split your big problemn into smaller ones.
First, you want to have items of your Price
column to be displayed according to a specified format, which you can achieve using a TableCellRenderer
(which, when rendering numbers, will use a NumberFormat
to display the correct number of decimals). As your table has only two columns of different types, the simplest solution is to use JTable#setDefaultRenderer(Class<?> columnClass, TableCellRenderer renderer)
. This way, your numbers will always be displayed using the correct number of decimals.
Second, for your edition issue, the same solution can be rather elegant : call JTable#setDefaultEditor(java.lang.Class, javax.swing.table.TableCellEditor)
and set as editor a component one that will, before commiting change to your table model, ensure the new value is a valid number (valid of course according to your rules).