Olá,
Gostaria de colocar um objeto BigDecimal em um JTable usando o retorno de um método, mas não estou conseguindo. Se eu remover o accessMethod eu consigo fazer funcionar … Estou usando o ObjectTableModel. Aqui vai o código.
@Entity
public class MaquinaLocacao {
@Transient
@Resolvable(colName="Valor", formatter=BigDecimalFormatter.class, accessMethod=MethodHandler.class)
private BigDecimal valorDoPeriodo;
@Transient
@Resolvable(colName="Total", formatter=BigDecimalFormatter.class, accessMethod=MethodHandler.class)
private BigDecimal valorTotal = new BigDecimal(0.0);
public BigDecimal getValorTotal() {
BigDecimal total = new BigDecimal("0.0");
// calculo
return total;
}
public BigDecimal getValorDoPeriodo() {
BigDecimal total = new BigDecimal("0.0");
// calculo
return total;
}
}
package br.com.MDLocacoes.View;
import java.math.BigDecimal;
import com.towel.bean.Formatter;
/**
* @author sonee
*
*/
public class BigDecimalFormatter implements Formatter {
/* Format the given date
* (non-Javadoc)
* @see com.towel.bean.Formatter#format(java.lang.Object)
*/
@Override
public String format(Object obj) {
if(obj != null && obj instanceof BigDecimal ){
StringBuilder str = new StringBuilder( ((BigDecimal)obj).toPlainString() );
// adiciona o separador de milhão
if( str.length() > 9 ){
str.insert( str.length() - 9, "," );
}
// adiciona o separador de milhar
if( str.length() > 6 ){
str.insert( str.length() - 6, "," );
}
// adiciona o simbolo $
str.insert( 0, "$ " );
return str.toString();
}else{
return "1";
}
}
/* Parse the given string into a valid date
* (non-Javadoc)
* @see com.towel.bean.Formatter#parse(java.lang.Object)
*/
@Override
public BigDecimal parse(Object obj) {
if( obj != null && obj instanceof String){
return new BigDecimal( ((String)obj).replace("$ ","").replace(",", ""));
}
return null;
}
/* (non-Javadoc)
* @see com.towel.bean.Formatter#getName()
*/
@Override
public String getName() {
return "BigDecimal";
}
}
AnnotationResolver resolver = new AnnotationResolver(MaquinaLocacao.class);
ObjectTableModel<MaquinaLocacao> tableMode = new ObjectTableModel<MaquinaLocacao>(resolver, "idDaMaquina,nomeDaMaquina,tipoDeLocacao,periodoDeLocacao,valorTotal");
tabelaDeMaquinas = new JTable();
tabelaDeMaquinas.setModel(tableMode);