jFormattedTextField valor monetario

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));

Dá uma olhada neste link:

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

Poderá te ajudar.

alguns dias depois…

[code]
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);   
	}   
}   

} [/code]

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

FLwS

[quote=feltraco]
alguns dias depois…

[code]
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);   
	}   
}   

} [/code]

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

FLwS[/quote]

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) {} }