jFormattedTextField valor monetario

3 respostas
K

Ola,

Alguem poderia me ajudar no seguinte: fiz uma mascara para valores monetarios em um jFormattedTextField, funcionou so que com alguns problemas. Quando o usuário apaga o valor que havia digitado e este esta depois da virgula e ele apaga ate antes da virgula por exemplo: digitou 10,20 quando apaga o 20 depois da virgula o numero fica com o seguinte valor 1000,00 e quando se apaga tudo fica o ,00 sem poder apagar, como poderia tratar isso? Segue o código abaixo.

DecimalFormat decimal = new DecimalFormat("###,###.00");

NumberFormatter formatoMonetario = new NumberFormatter(decimal);

formatoMonetario.setFormat(decimal);

formatoMonetario.setAllowsInvalid(false);

jFormattedTextField.setFormatterFactory( new DefaultFormatterFactory(formatoMonetario));

3 Respostas

Petronio_Braga

Dá uma olhada neste link:

http://www.java2s.com/Code/Java/Swing-JFC/Formatted-TextField.htm

Poderá te ajudar.

feltraco

alguns dias depois.....

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class MeuDocument extends PlainDocument{   

	public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {   
		String texto = getText(0,getLength());   
		if(texto.length()<7){   
			remove(0,getLength());   
			StringBuffer strBuf = new StringBuffer(texto.replaceAll(",","")+str);   
			if(strBuf.length()<3)   
				strBuf. insert(0,",");   
			else   
				strBuf.insert(strBuf.length()-2,",");   
			super.insertString(0, strBuf.toString(), a);   
		}   
	}   
}

dai vc instancia um textFiel normal e
da um .setDocument(new MeuDocument())

FLwS

thiagofesta
feltraco:
alguns dias depois.....
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class MeuDocument extends PlainDocument{   

	public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {   
		String texto = getText(0,getLength());   
		if(texto.length()<7){   
			remove(0,getLength());   
			StringBuffer strBuf = new StringBuffer(texto.replaceAll(",","")+str);   
			if(strBuf.length()<3)   
				strBuf. insert(0,",");   
			else   
				strBuf.insert(strBuf.length()-2,",");   
			super.insertString(0, strBuf.toString(), a);   
		}   
	}   
}

dai vc instancia um textFiel normal e
da um .setDocument(new MeuDocument())

FLwS

Dessa forma ai permite letras, use assim:
@Override
    public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
        try {
            Integer.parseInt(str);
            String texto = getText(0, getLength());
            if(texto.length() < 15) {
                remove(0, getLength());
                StringBuffer strBuf = new StringBuffer(texto.replaceAll(",", "") + str);
                if(strBuf.length() < 3) {
                    strBuf.insert(0, ",");
                } else {
                    strBuf.insert(strBuf.length()-2, ",");
                }
                super.insertString(0, strBuf.toString(), a);
            }
        } catch(Exception e) {}
	}
Criado 21 de outubro de 2006
Ultima resposta 9 de jul. de 2009
Respostas 3
Participantes 4