Tenho um problema ao editar uma célula numa JTable.O formatter funciona legal, mas ao clicar na célula para editar, ele come alguns valores: Se a célula tem o valor 0.900, ao clicar para editar, o valor passa para 0.009 só de clicar em cima!
Meu cell formatter é esse:
[code]
public class FormatTableCellEditor extends AbstractCellEditor implements TableCellEditor {
JComponent component =null;
List lista=null;
public FormatTableCellEditor(List l){
super();
this.lista=l;
}
public Component getTableCellEditorComponent(JTable table, Object value,boolean isSelected, int rowIndex, int vColIndex) {
//‘value’ is value contained in the cell located at (rowIndex, vColIndex)
if (isSelected) {
// cell (and perhaps other cells) are selected
}
for(int j=0;j<lista.size();j++){
Client cadastrado=lista.get(j);
if(j==rowIndex){
component= formatarCampo(cadastrado.getRange());
}
}
component.setForeground(Color.BLACK);
component.setBackground(table.getBackground());
// Configure the component with the specified value
((JTextField)component).setText((String)value);
// Return the configured component
return component;
}
// 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();
}
@SuppressWarnings("serial")
public JComponent formatarCampo(int range){
JComponent component=null;
if(range==3){
component = new JNumberFormatField(new DecimalFormat("#,##0.000")){{setLimit(9);}};
}else if(range==2){
component = new JNumberFormatField(new DecimalFormat("#,##0.00")){{setLimit(8);}};
}else if(range==1){
component = new JNumberFormatField(new DecimalFormat("#,##0.0")){{setLimit(8);}};
}
return component;
}
}[/code]
Meu model é o ObjectTableModel do Towell e eu uso assim o formatter do towell:
@Resolvable(colName = "Valor Médio" , formatter = BigDecimalFormatter.class)
private BigDecimal valor_medio;
E minha classe de formatação:
[code]
public class BigDecimalFormatter implements Formatter {
DecimalFormat df = new DecimalFormat ("#,##0.00");
BigDecimal b1 =null;
@Override
public Object format(Object obj) {
BigDecimal bd = (BigDecimal) obj;
if(bd == null)
return “0.0”;
return bd.toString();
}
@Override
public String getName() {
return “BigDecimal”;
}
@Override
public Object parse(Object obj) {
try{
df.setParseBigDecimal(true);
return df.parseObject(obj.toString());
}catch(Exception e){
e.printStackTrace();
}
return "0.0";
}
}[/code]
Onde está o erro?