Validate jFormattedTextField with Mask

1

I'm using jFormattedTextField with Mascara but I do not know how to validate if the entire field has been populated.

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: Ok

// 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();
            }
    
asked by anonymous 09.12.2015 / 12:43

1 answer

2

else if(ftCPF.getText().trim().length() < 14) { JOptionPane.showMessageDialog(null, "Por favor, informar campo CPF", "Informação", JOptionPane.INFORMATION_MESSAGE); ftCPF.requestFocusInWindow();

Try this, if the length of the string that holds cpf is different from 14 (which is the number of digits + the characters of the mask) then cpf is not valid.

    
09.12.2015 / 21:19