Colocar máscara em textfield

4 respostas
DANIEL35

Olá, gostaria de saber como colocar máscara em um campo que receberá data, rg e cpf. ex: data: dd/mm/aaaa
rg: xx.xxx.xxx-x
cpf: xxx.xxx.xxx-xx

4 Respostas

L

exemplo vê se isso ajuda flou!

lblDataNasc1.setText("Data");

        try {
            txtDataExpedicao.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txtDataExpedicao.setFocusLostBehavior(javax.swing.JFormattedTextField.PERSIST);
        txtDataExpedicao.setName("data");

[color=red]cpf:[/color]

lblCPF.setText("CPF");

   

        try{
            txtCPF.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txtCPF.setFocusLostBehavior(javax.swing.JFormattedTextField.PERSIST);
        txtCPF.setName("cpf"); 
        txtCPF.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
Ratao

Olhe:
http://java.sun.com/docs/books/tutorial/uiswing/components/formattedtextfield.html

marcelogn

Observe tmb os caracteres usados pra máscaras:

- Números

U - Qlqr caracter, passando td pra maiusculo
L - Qlqr caracter, passando td pra minusculo
A - Caracter e número

    • Qlqr coisa…

pra + detelhes:
http://www.java2s.com/Tutorial/Java/0240__Swing/0320__JFormattedTextField.htm
:!:

DANIEL35

Vou fazer os testes aqui, e já retornarei a resposta. Obrigado.

Criado 1 de junho de 2010
Ultima resposta 1 de jun. de 2010
Respostas 4
Participantes 4