Java - Capturing data from JFormattedTextField and Validate

1

Good evening,

I have the following problem. I do not know how to validate if the field of the JFormatted TextField has something written or not, so long as the formatting ###. ###. ### - ## is not considered valid or is required is actually 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");
    
asked by anonymous 05.12.2015 / 22:07

1 answer

1

Good afternoon Junior

If the CPF has a class for itself, the validation can be done within its own class, have a method to verify if the value received by the TextField is null or empty, through a String.

Other types of validations can be made using the methods of the String class itself, and along with String cpf you can also validate using RegEx.

For example, by accepting only [a-zA-Z]* or just [0-9]* numbers or only letter and numbers [a-zA-Z0-9]*

link

link

Ex:

public class CPF {

    private String valor;

    public CPF(String cpf) {
        if(cpf == null || cpf.isEmpty()) {
            throw new RuntimeException("Não aceita campo vazio");
        }
        this.valor = cpf; //Caso não estiver nulo ou vazio, ele atribui o valor para o atributo da classe
    }

    public String getValor() {
        return valor;
    }
}

Other checks / validations can be done inside the constructor or in any other method.

    
10.12.2015 / 19:13