Escolar Documentos
Profissional Documentos
Cultura Documentos
linguagem.
1 package com.exemplo;
2
3 import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
4 import javax.swing.text.PlainDocument;
5
6 public class TamanhoFixoJTextField extends PlainDocument {
7
8 private int tamMax;
9
10 public TamanhoFixoJTextField(int tamMax) {
super();
11 this.tamMax = tamMax;
12 }
13
14 public void insertString(int offset, String str, AttributeSet
15attr)
16 throws BadLocationException {
17
if (str == null)
18 return;
19
20 //Define a condição para aceitar qualquer número de
21caracteres
22 if (tamMax <= 0)
23 {
super.insertString(offset, str, attr);
24 return;
25 }
26
27 int tam = (getLength() + str.length());
28
29 //Se o tamanho final for menor, chama insertString()
30 aceitando a String
if (tam <= tamMax)
31 super.insertString(offset, str, attr);
32 }
33
34}
35
36
1 package com.exemplo;
2
3 import javax.swing.JFrame;
4 import javax.swing.JTextField;
5
public class TestandoJTextField extends JFrame {
6
7 private static final long serialVersionUID = 1L;
8
9 private JTextField txtExemplo;
10
11 public static void main(String[] args)
12 {
13 TestandoJTextField field = new TestandoJTextField();
14 field.testaJTextField();
}
15
16
private void testaJTextField() {
17 this.setTitle("Exemplo");
18 this.setSize(200, 180);
19
20 //Definimos o tamanho padrão do JTextField
21 txtExemplo = new JTextField(10);
22
//Passamos para o construtor o número máximo de caracteres
23
aceitos
24 txtExemplo.setDocument(new TamanhoFixoJTextField(5));
25
26 this.getContentPane().add(txtExemplo, "North");
27
28 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
29
30 setVisible(true);
31 }
32
}
33
34
35
1 package com.exemplo;
2
import javax.swing.text.AttributeSet;
3 import javax.swing.text.BadLocationException;
4 import javax.swing.text.PlainDocument;
5
6 public class TamanhoFixoJTextField extends PlainDocument {
7
8 private int tamMax;
9
10 public TamanhoFixoJTextField(int tamMax) {
super();
11 this.tamMax = tamMax;
12 }
13
14 public void insertString(int offset, String str, AttributeSet
15attr)
16 throws BadLocationException {
17
if (str == null)
18 return;
19
20 //Define a condição para aceitar qualquer número de
21caracteres
22 if (tamMax <= 0)
{
23
super.insertString(offset, str, attr);
24 return;
25 }
26
27 int tam = (getLength() + str.length());
28
29 //Se o tamanho final for menor, chama insertString()
aceitando a String
30 if (tam <= tamMax) {
31 super.insertString(offset, str, attr);
32 } else {
33 //Caso contrário, limita a string e envia para
insertString() que aceita a string
34 if (getLength() == tamMax) return;
35 String novaStr = str.substring(0, (tamMax -
36getLength()));
37 super.insertString(offset, novaStr, attr);
38 }
}
39
40}
41
42
1 package com.exemplo;
2 import java.awt.Container;
3 import java.text.ParseException;
4
5 import javax.swing.JFormattedTextField;
6 import javax.swing.JFrame;
7 import javax.swing.JLabel;
import javax.swing.text.MaskFormatter;
8
9 public class TestandoJFormattedTextField extends JFrame {
10
11 private static final long serialVersionUID = 1L;
12
13 public static void main(String[] args)
14 {
15 TestandoJFormattedTextField field = new
TestandoJFormattedTextField();
16 field.testaJFormattedTextField();
17 }
18
19 private void testaJFormattedTextField() {
20 Container janela = getContentPane();
21 setLayout(null);
22
//Define os rótulos dos botões
23 JLabel labelCep = new JLabel("CEP: ");
24 JLabel labelTel = new JLabel("Telefone: ");
25 JLabel labelCpf = new JLabel("CPF: ");
26 JLabel labelData = new JLabel("Data: ");
27 labelCep.setBounds(50,40,100,20);
labelTel.setBounds(50,80,100,20);
28 labelCpf.setBounds(50,120,100,20);
29 labelData.setBounds(50,160,100,20);
30
31 //Define as máscaras
32 MaskFormatter mascaraCep = null;
MaskFormatter mascaraTel = null;
33 MaskFormatter mascaraCpf = null;
34 MaskFormatter mascaraData = null;
35
36 try{
37 mascaraCep = new MaskFormatter("#####-###");
38 mascaraTel = new MaskFormatter("(##)####-####");
mascaraCpf = new MaskFormatter("#########-##");
39 mascaraData = new MaskFormatter("##/##/####");
40 mascaraCep.setPlaceholderCharacter('_');
41 mascaraTel.setPlaceholderCharacter('_');
mascaraCpf.setPlaceholderCharacter('_');
42 mascaraData.setPlaceholderCharacter('_');
43 }
44 catch(ParseException excp) {
45 System.err.println("Erro na formatação: " +
46 excp.getMessage());
System.exit(-1);
47 }
48
49 //Seta as máscaras nos objetos JFormattedTextField
50 JFormattedTextField jFormattedTextCep = new
51JFormattedTextField(mascaraCep);
52 JFormattedTextField jFormattedTextTel = new
JFormattedTextField(mascaraTel);
53 JFormattedTextField jFormattedTextCpf = new
54JFormattedTextField(mascaraCpf);
55 JFormattedTextField jFormattedTextData = new
56 JFormattedTextField(mascaraData);
jFormattedTextCep.setBounds(150,40,100,20);
57 jFormattedTextTel.setBounds(150,80,100,20);
58 jFormattedTextCpf.setBounds(150,120,100,20);
59 jFormattedTextData.setBounds(150,160,100,20);
60
61 //Adiciona os rótulos e os campos de textos com
62máscaras na tela
janela.add(labelCep);
63 janela.add(labelTel);
64 janela.add(labelCpf);
65 janela.add(labelData);
66 janela.add(jFormattedTextCep);
67 janela.add(jFormattedTextTel);
janela.add(jFormattedTextCpf);
68 janela.add(jFormattedTextData);
69 setSize(400, 250);
70 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
71 setVisible(true);
}
72
73}
74
75
76
77
78
79
80
Se tentarmos entrar com letras onde apenas se aceita número veremos que os
caracteres não serão aceitos no campo.