//Olá pessoal, estou com o seguinte problema: Quando eu preencho o campo data, depois que clico em limpar, o conteúdo
do JTextFormatted é limpo, porém se depois dele estar limpo, se este campo obter o foco e eu dar um tab com o foco nele
, mesmo que não tenha digitado nada, o valor que tinha informado nele anteriormente aparecerá, como se o conteúdo que eu
limpei anteriormente estivesse na memória. Alguém sabe dizer o que está acontecendo?
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;
public class UsingJFormattedTextField extends JFrame {
private JLabel nomeLabel;
private JLabel dataLabel;
private MaskFormatter mascaraData;
private JFormattedTextField dataJForTextField;
private JTextField nomeTextField;
private JButton cadastrarButton;
private JButton limparButton;
public UsingJFormattedTextField() throws ParseException{
super("Usando o JFormattedTextField");
montarTela();
}
private void montarTela() throws ParseException{
montarJanela();
montarCampoNome();
montarCampoData();
montarCadastrarButton();
montarLimparButton();
}
private void montarLimparButton() {
limparButton = new JButton("Limpar");
limparButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dataJForTextField.setText("");
nomeTextField.setText("");
}
});
this.add(limparButton);
}
private void montarCampoNome() {
nomeLabel = new JLabel("Nome: ");
this.add(nomeLabel);
nomeTextField = new JTextField(20);
this.add(nomeTextField);
}
private void montarCadastrarButton() {
cadastrarButton = new JButton("Cadastrar");
this.add(cadastrarButton);
}
private void montarCampoData() throws ParseException{
dataLabel = new JLabel("Data de admissão: ");
this.add(dataLabel);
mascaraData = new MaskFormatter("##-##-####");
mascaraData.setPlaceholderCharacter('_');
dataJForTextField = new JFormattedTextField(mascaraData);
this.add(dataJForTextField);
}
private void montarJanela() {
this.setSize(500, 500);
this.setLayout(new FlowLayout());
}
public static void main(String[] args) throws ParseException{
UsingJFormattedTextField usingJFormattedTextField = new UsingJFormattedTextField();
usingJFormattedTextField.setVisible(true);
usingJFormattedTextField.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}