Olá pessoal, como faço para que no campo data o usuário não precise digitar “/”, ou seja, inserir a barra aoutomaticamente conforme ele informe os números correspondente à data?
[code]ftfCPF = new javax.swing.JFormattedTextField();
ftfCPF.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));[/code]
este é pra cpf, mas vc pode trocar os pontos por barra.
indica que apenas números podem ser aceitos.
e no caso ao invés de TextField, uso o JFormattedTextField.
Obrigado lordtiago.
posta como resolvido!
Depois de pensar e repensar resolvi fazer de outro jeito, pois pense bem, o ideal é que o usuário não precise inserir a barra, porque a não ser que ele já tenha sido treinado para operar o sistema, ele não irá adivinhar que se ele digitar por exemplo ‘10’, o sistema irá inserir a barra automaticamante.
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JFormattedTextField.AbstractFormatterFactory;
import javax.swing.text.MaskFormatter;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
public class TesteCampoData extends JFrame{
JFormattedTextField txtDataAdmissao, txtDataDemissao,
txtCep, txtTelefone, txtTelRec;
JTextField txtNome;
JLabel lblNome, lblAdmissao, lblDemissao, lblCep, lblTelefone, lblTelRec;
JButton btnInserir, btnCancelar;
MaskFormatter mascara, mascaraCep, mascaraTelefone;
public TesteCampoData() {
super("Trabalhando com campo data");
montaJanela();
montaCampos();
mostraJanela();
}
public void montaCampos() {
try {
mascara = new MaskFormatter("##/##/####");
mascara.setPlaceholderCharacter('_');
mascaraCep = new MaskFormatter("#####-###");
mascaraCep.setPlaceholderCharacter('_');
mascaraTelefone = new MaskFormatter("(##) - ####-####");
mascaraTelefone.setPlaceholderCharacter('_');
lblNome = new JLabel("Nome: ");
this.add(lblNome);
txtNome = new JTextField(20);
this.add(txtNome);
lblTelefone = new JLabel("Telefone");
this.add(lblTelefone);
txtTelefone = new JFormattedTextField(mascaraTelefone);
this.add(txtTelefone);
lblTelRec = new JLabel("Tel. Rec. : ");
this.add(lblTelRec);
//observe que a mesma mascara usada no campo "txtTelefone" é também
//usada na caixa de texto txtTelRec
txtTelRec = new JFormattedTextField(mascaraTelefone);
this.add(txtTelRec);
lblCep = new JLabel("Cep:");
this.add(lblCep);
txtCep = new JFormattedTextField(mascaraCep);
this.add(txtCep);
lblAdmissao = new JLabel("Admissão: ");
this.add(lblAdmissao);
txtDataAdmissao = new JFormattedTextField(mascara);
this.add(txtDataAdmissao);
lblDemissao = new JLabel("Demissão: ");
this.add(lblDemissao);
txtDataDemissao = new JFormattedTextField(mascara);
this.add(txtDataDemissao);
btnInserir = new JButton("Inserir");
this.add(btnInserir);
btnCancelar = new JButton("Cancelar");
btnCancelar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
limpaCampos(txtNome, txtTelefone, txtTelRec, txtCep, txtDataAdmissao, txtDataDemissao);
}
});
this.add(btnCancelar);
} catch (Exception e) {
e.printStackTrace();
}
}
private void limpaCampos(JTextField... txt) {
for (JTextField t : txt) {
t.setText(null);
}
}
public void montaJanela() {
this.setSize(500, 500);
this.setLayout(new FlowLayout());
this.setLocationRelativeTo(null);
}
public void mostraJanela() {
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TesteCampoData();
}
}
levimendes Parabéns! o teu código é totalmente aplicável.
Eu uso o netbeans e por isso meu JFormattedTextField ja está pronto.
Quem tem o mesmo caso que eu faça o seguinte:
Acesse o recurso PERSONALIZAR CODIGO clicando com o botao direito no seu JFormattedTextField,
agora faça com que seu código fique assim:
txtCliNasc = new javax.swing.JFormattedTextField();
try {
MaskFormatter mascara = new MaskFormatter("##/##/####");
mascara.setPlaceholderCharacter(’_’);
txtCliNasc.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(mascara));
} catch (java.text.ParseException ex) {
ex.printStackTrace();
}
Funcionou legal
levimendes, muito obrigado!