1
I’m using jFormattedTextField with Mascara but I don’t know how to validate if the whole field has been filled.
jFormattedTextField
// CPF
JLabel lblCpf = new JLabel("CPF");
contentPane.add(lblCpf, "cell 0 10");
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 11,growx");
Button: Okay
// Botão > Ok
JButton btnOk = new JButton("Ok");
btnOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Validar Campos
if(tfNome.getText().length() == 0) {
JOptionPane.showMessageDialog(null, "Por favor, informar campo Nome", "Informação", JOptionPane.INFORMATION_MESSAGE);
tfNome.requestFocusInWindow();
} else if(pfSenha.getPassword().length == 0) {
JOptionPane.showMessageDialog(null, "Por favor, informar campo Senha", "Informação", JOptionPane.INFORMATION_MESSAGE);
pfSenha.requestFocusInWindow();
} else if(pfCSenha.getPassword().length == 0) {
JOptionPane.showMessageDialog(null, "Por favor, informar campo Confirmação de Senha", "Informação", JOptionPane.INFORMATION_MESSAGE);
pfCSenha.requestFocusInWindow();
} else if(cbGrupo.getSelectedItem().equals("")) {
JOptionPane.showMessageDialog(null, "Por favor, informar campo Grupo.", "Informação", JOptionPane.INFORMATION_MESSAGE);
cbGrupo.requestFocusInWindow();
} else if(cbEstado.getSelectedItem().equals("")) {
JOptionPane.showMessageDialog(null, "Por favor, informar campo Estado", "Informação", JOptionPane.INFORMATION_MESSAGE);
cbEstado.requestFocusInWindow();
} else if(ftCPF.getText().equals(null)) {
JOptionPane.showMessageDialog(null, "Por favor, informar campo CPF", "Informação", JOptionPane.INFORMATION_MESSAGE);
ftCPF.requestFocusInWindow();
} else if(cbPergunta.getSelectedItem().equals("")) {
JOptionPane.showMessageDialog(null, "Por favor, informar campo Pergunta Secreta", "Informação", JOptionPane.INFORMATION_MESSAGE);
cbPergunta.requestFocusInWindow();
} else if(pfResposta.getPassword().length == 0) {
JOptionPane.showMessageDialog(null, "Por favor, informar campo Resposta Secreta", "Informação", JOptionPane.INFORMATION_MESSAGE);
pfResposta.requestFocusInWindow();
}
solved with Trim() and < 14 digits. Thanks champion!!
– Júnior Nascimento
Good, I just don’t understand why Trim(), if I remember it takes out the spaces that are before or after the string, correct?
– Bruno Brito
Correct, I didn’t understand it either more he only understands it after I played Tim(). Very strange this. Thank you!!
– Júnior Nascimento