0

I'm trying to verify that a user has entered the correct number of integers in the phonebook to complete the contact and if not then I would issue an error message like I do when a name is not entered. Problem I run into is that the field has been formatted with a maskformatter and I believe thats why no matter what I enter variable ftf1 returns 14 as the number of characters. Is there any easy way to check if all 10 numbers have indeed been entered?

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;
import java.text.ParseException;


public class PhoneBook implements ActionListener {
    JFrame frame;

    JTextField textField, textField1;

    JButton createButton, deleteButton;

    JPanel panel;

    Font pbFont = new Font("PB", Font.BOLD, 15);

    JLabel label, label1, label2;

    JFormattedTextField ftf1;
    MaskFormatter format1;

    JTextArea message;

    char operation;

    PhoneBook() throws ParseException {
        frame = new JFrame("Phone Book");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);
        frame.setLayout(null);
        frame.setResizable(false);

        label = new JLabel();
        label.setBounds(150,-35,100,100);
        label.setText("Contact");
        label.setFont(pbFont);

        label1 = new JLabel();
        label1.setBounds(30, 10,100,50);
        label1.setText("Name:");
        label1.setFont(pbFont);

        label2 = new JLabel();
        label2.setBounds(30, 60,100,50);
        label2.setText("Phone:");
        label2.setFont(pbFont);

        textField = new JTextField();
        textField.setBounds(80,25,200,21);
        textField.setFont(pbFont);
        textField.setEditable(true);

        format1 = new MaskFormatter("(###) ###-####");
        ftf1 = new JFormattedTextField(format1);
        ftf1.setBounds(80,75,200,21);
        ftf1.setFont(pbFont);
        ftf1.setEditable(true);

        message = new JTextArea(10,10);
        message.setFont(pbFont);
        message.setBounds(25,210,200,50);


        /* textField1 = new JTextField();
        textField1.setBounds(80,75,200,21);
        textField1.setFont(pbFont);
        textField1.setEditable(true); */

        panel = new JPanel();
        panel.setBounds(25,110,255,50);
        panel.setLayout(new GridLayout(1,2,125,20));
        ///panel.setBackground(Color.BLUE);

        createButton = new JButton("+");
        deleteButton = new JButton("-");

        createButton.addActionListener(this);
        createButton.setFont(pbFont);
        createButton.setFocusable(false);
        createButton.setOpaque(true);
        createButton.setForeground(Color.GREEN);


        deleteButton.addActionListener(this);
        deleteButton.setFont(pbFont);
        deleteButton.setFocusable(false);
        deleteButton.setOpaque(true);
        deleteButton.setForeground(Color.red);


        panel.add(createButton);
        panel.add(deleteButton);




        frame.add(panel);
        frame.add(textField);
        //frame.add(textField1);
        frame.add(ftf1);
        frame.add(label);
        frame.add(label1);
        frame.add(label2);
        frame.add(message);
        frame.setVisible(true);




    }




    public static void main(String[] args) throws ParseException {
        PhoneBook directory = new PhoneBook();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        TreeMap<String, String> contacts =
                new TreeMap<String, String>();

        boolean x = false;

        String s = ftf1.getText();
        System.out.print(s.length());



        if(e.getSource() == createButton) {
            if(textField.getText().isEmpty()) {
                JOptionPane.showMessageDialog(frame, "Enter Name & Phone Number.","Contact Creation", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}
kleopatra
  • 51,061
  • 28
  • 99
  • 211

1 Answers1

0

you are correct. The formatter is adding spaces to the empty characters. you can remove all spaces and check the length.

String s = ftf1.getText();
s = s.replaceAll("\\s+", "");
System.out.print(s.length());

The expected length after removing spaces is 12 (10 numbers+2 brackets). You can validate if s.length() < 12

Or you can,

s =s.replaceAll("[^0-9]+", "");

only keep numbers and validate if length < 10

sanurah
  • 976
  • 1
  • 6
  • 16