public class MonetarioDocument extends PlainDocument {
public static final int NUMERO_DIGITOS_MAXIMO = 12;
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
String texto = getText(0, getLength());
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (!Character.isDigit(c)) {
return;
}
}
if(texto.length() < NUMERO_DIGITOS_MAXIMO){
super.remove(0, getLength());
texto = texto.replace(".", "").replace(",", "");
StringBuffer s = new StringBuffer(texto + str);
if (s.length() > 0 && s.charAt(0) == '0') {
s.deleteCharAt(0);
}
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");
}
}
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);
}
}
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);
}
}
Document Dinamico
5 Respostas
Acho que fui euque pus esse código aqui há um tempo atrás, mas sua explicação tá confusa…
Vc tá na verdade, escolhendo a casa decimal numa JCombobox, e de acordo com essa escolha, permitir a digitação(creio que num JTextField) usando esse Document com o númerode casas decimais escolhida é isso???
Algo + ou - do tipo(usando JFormattedTextField só para ilustrar):
private class ComboRangeListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
int escolhido=rangeCombo.getSelectedIndex();
if(escolhido == 0){
valorField = new JFormattedTextField(new DecimalFormat("#,###,##0.0"));
}else if(escolhido ==1){
valorField = new JFormattedTextField(new DecimalFormat("#,###,##0.00"));
}else if(escolhido==2){
valorField = new JFormattedTextField(new DecimalFormat("#,###,##0.000"));
}
}
}
Só que usando PlainDocument é isso? :?: :?: :?:
Só que usando PlainDocument é isso?
Cara é exatamente isso que eu quero, só que permitindo de 1 até 3 casas decimais!Com JFormatted não funciona bem!
Pode me ajudar modificando aquela classe Document para permitir isso?
Alguém poda me ajudar, tipo:
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);
}
Tô meio perdido… quero poder fazer:
textField.setDocument(MeuDocument(1))//umacasa decimal, tipo 290,1
textField.setDocument(MeuDocument(3)) //permite a digitação de 3 casas como 290,100
PaulH,
vc terá que criar um construtor para receber o tamanho e o limite de casas decimais para controlar isso.Já tive problemas no passado com campos usando Documents dinâmicos(davam problemas com troca de foco ná época).
Dê uma olhada nessa abordagem que pode clarear a sua mente:
http://www.guj.com.br/java/81197-tem-como-eu-digitar-e-ele-ja-ir-formatando-para-moeda-/2
Cara, muito obrigado!Funcionou!