Mascaras

3 respostas
fabioebner

Pessoal alguem ae tem aquelas mascaras de DATA E VALOR?? na qual eu vou digitando em um JTextField e ele vai arrumando??

obs tem q ser em JTextField

3 Respostas

fabiozoroastro

##/##/####

É isso que você quer?

fabioebner

entao mas isso funciona no JFormattedText Field… eu queria um metodo q vai colocando as mascaras a medida q o usuario vai digitando. como se e feito pela net em alguns campos

fabiozoroastro

Um JFormattedTextField é um JTextField, portanto, você terá o JTextField com a instância do JFormattedTextField sem problemas. Pelo que entendi, você que utilizar filtros e não máscara.
Dê uma olhada nessa implementação que eu peguei na internet:

class MyDocumentFilter extends DocumentFilter{
 public void replace(FilterBypass fb, int offset, int length,
      String text, AttributeSet attrs) throws BadLocationException {
 
 //The current length minus the no.of characters deleted plus the
 // number of characters inserted is the final length in the textfield
 //and should not be more than 20
 if ((fb.getDocument().getLength() + text.length() - length) <= 20){
	super.replace(fb, offset,length, text, attrs);
 }
 else{
 	//Let's beep if the user tries to enter more characters
 	Toolkit.getDefaultToolkit().beep();
 }
}
public void insertString(FilterBypass fb, int offset,
       String string, AttributeSet attr) throws BadLocationException {
 
  //While inserting the current length plus the number of characters inserted
  //is the final length in the textfield and should not be more than 20
  if ((fb.getDocument().getLength() + string.length()) <= 20){
    super.insertString(fb, offset,string, attr);
  }
  else{
    //Let's beep if the user tries to enter more characters
   Toolkit.getDefaultToolkit().beep();
  }
 }
}
((AbstractDocument)textField.getDocument())
.setDocumentFilter(new MyDocumentFilter());

É isso aí. Abraço.

Criado 1 de dezembro de 2008
Ultima resposta 1 de dez. de 2008
Respostas 3
Participantes 2