É o seguinte, tenho um campo de valores em dinheiro onde não posso deixar que o usuário digite letras, valores negativos e etc,
fiz uma classe de Document para usar como mascara, para o usuário nao digitar estes caracteres, esta classe aqui:
package sgia.util;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
public class Mascaras extends PlainDocument {
private int numMaximoDigitos = 30;
public Mascaras(int numMaximoDigitos) {
this.numMaximoDigitos = numMaximoDigitos;
}
@Override
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
//Recupera o valor que j� est� no campo
String texto = getText(0, getLength());
//Verifica se o novo elemento � um numero
str = str.replace(",", ".");
if (str.length() > 1 && str.charAt(str.length() - 1) == '.') {
str = str + "0";
}
if (str.length() > 2 && str.charAt(str.length() - 2) == '.') {
str = str + "0";
}
str = str.replace(".", "");
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (!Character.isDigit(c)) {
return;
}
}
//Limita o numero de caracteres
if (texto.length() < numMaximoDigitos) {
//Limpa o campo
super.remove(0, getLength());
//Retira pontos e virgula e acrescenta o novo numero
texto = texto.replace(".", "").replace(",", "");
StringBuffer s = new StringBuffer(texto + str);
//Retira os zeros � esquerda
if (s.length() > 0 && s.charAt(0) == '0') {
s.deleteCharAt(0);
if (s.length() > 0 && s.charAt(0) == '0') {
s.deleteCharAt(0);
if (s.length() > 0 && s.charAt(0) == '0') {
s.deleteCharAt(0);
}
}
}
//Coloca os zeros � esquerda
if (s.length() < 3) {
if (s.length() < 1) {
s.insert(0, "000");
} else if (s.length() < 2) {
s.insert(0, "00");
} else {
s.insert(0, "0");
}
}
//Coloca virgula e ponto
s.insert(s.length() - 2, ",");
if (s.length() > 6) {
s.insert(s.length() - 6, ".");
}
if (s.length() > 10) {
s.insert(s.length() - 10, ".");
}
super.insertString(0, s.toString(), a);
}
}
@Override
public void remove(int offset, int length) throws BadLocationException {
super.remove(offset, length);
String texto = getText(0, getLength());
texto = texto.replace(",", "");
texto = texto.replace(".", "");
super.remove(0, getLength());
insertString(0, texto, null);
}
}
até ai tudo bem, funciono certinho a mascara e tals, o problema vem na hora de converter o valor para BigDecimal, ele não converte e da erro, mais não sei porque,
o erri exibido é esse:
java.lang.NumberFormatException
at java.math.BigDecimal.<init>(BigDecimal.java:534)
at java.math.BigDecimal.<init>(BigDecimal.java:728)
at sgia.formularios.cadastros.JanelaCaixa.gravarDados(JanelaCaixa.java:824)
at sgia.formularios.cadastros.JanelaCaixa.btAbrirActionPerformed(JanelaCaixa.java:684)
at sgia.formularios.cadastros.JanelaCaixa.access$200(JanelaCaixa.java:25)
se alguem ai souber ideia do pq desse erro eu agradeço.
Obrigado.