Java - How to Capture Data from Jformattedtextfield and Validate

Asked

Viewed 910 times

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");

1 answer

1

Good afternoon Junior

If the CPF has a class for representing itself, the validation can be done within its own class, have a method for checking whether the value received by Textfield is null or empty, through a String.

Other types of validations can be done with the String class methods themselves, and together with the String Cpf can also use Regex.

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

https://docs.oracle.com/javase/8/docs/api/java/lang/String.html

https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html

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 within the constructor or in any other method.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.