1
Good night,
I have the following problem. I don’t know how to validate if the Jformatted Textfield field has something written or not, so much so that the formatting ##. ###. ###-## is not considered valid or is it really necessary is filled by numbers.
Jformattedtextfield
// CPF
JLabel lblCpf = new JLabel("CPF");
contentPane.add(lblCpf, "cell 0 0");
JFormattedTextField ftCpf = new JFormattedTextField();
MaskFormatter mfCPF = new MaskFormatter();
try {
mfCPF.setMask("###.###.###-##");
mfCPF.install(ftCpf);
ftCpf.setText("");
} catch (ParseException e1) {
e1.printStackTrace();
}
contentPane.add(ftCpf, "cell 0 1,growx");
Ok button
// Botão > Ok
JButton btnOk = new JButton("Ok");
btnOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// Validar Campos
if(ftCpf.equals("###.###.###-##") && tfPergunta.getText().equals("") && tfResposta.getText().equals(""))
JOptionPane.showMessageDialog(null, "Por favor, preenchao todos os campos acima", "Informação", JOptionPane.INFORMATION_MESSAGE);
else if(tfPergunta.getText().equals(""))
JOptionPane.showMessageDialog(null, "Por favor, preenchao campo de Pergunta.", "Informação", JOptionPane.INFORMATION_MESSAGE);
else if(tfResposta.getText().equals(""))
JOptionPane.showMessageDialog(null, "Por favor, preenchao campo da Resposta.", "Informação", JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null, "Campos preenchidos.", "Informação", JOptionPane.INFORMATION_MESSAGE);
// Capturar Dados
String cpf = mfCPF.getMask();
String pergunta = tfPergunta.getText();
String resposta = tfResposta.getText();
}
});
panel.add(btnOk, "cell 0 0,grow");