Validation of text fields and contact no text field

3.6k Views Asked by At

I have a JFrame consisting of some text fields (10) and a TextArea. I want to validate all the text fields and see if they are not empty and also check if a 10 digit contact no is entered in one of the text field. After checking the text fields, I want to enable a submit button which is used to submit all this dat to my database.

I used following code with adding the text area condition but it is not working,gives the error:- Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

Here is the code i used but it is not working:-

public class DataEntered1 implements DocumentListener
{
    private JButton button;
    List<JTextField> txtfields=new ArrayList<JTextField>();
    JTextArea ta;
    public DataEntered1(JButton dbadd)
    {
        this.button=dbadd;
    }
    public void addTextField(JTextField txtfield)
    {
        txtfields.add(txtfield);
        txtfield.getDocument().addDocumentListener(this);
    }
    public void addTextArea(JTextArea ta)
    {
        this.ta=ta;
        ta.getDocument().addDocumentListener(this);
    }
    public boolean isDataEntered()
    {
        for(JTextField txtfield:txtfields)
        {
            if(txtfield.getText().length()==0)
            return false;
        }
        return true;
    }
    public boolean isData()
    {
        if(ta.getText().trim().length()==0)
        {
             return false;
        }
        return true;
    }
    public void insertUpdate(DocumentEvent e) {
    checkdata();
    }
    public void removeUpdate(DocumentEvent e) {
    checkdata();
    }
    public void changedUpdate(DocumentEvent e) {
    checkdata();
    }
    public void checkdata(){
    Boolean d1=isDataEntered();
    Boolean d2=isData();
    if(d1&&d2)
    button.setEnabled(true);
    }

}

2

There are 2 best solutions below

3
On BEST ANSWER

Can anyone help me about enabling the button after validating all the text fields?

Here is a general purpose class that will enable/disable a button as text is added/removed from a group of text fields.

It adds a DocumentListener to the Documenent of each text field. The button will then ben enable only when text has been entered into each Document:

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.*;

public class DataEntered implements DocumentListener
{
    private JButton button;
    private List<JTextField> textFields = new ArrayList<JTextField>();

    public DataEntered(JButton button)
    {
        this.button = button;
    }

    public void addTextField(JTextField textField)
    {
        textFields.add( textField );
        textField.getDocument().addDocumentListener( this );
    }

    public boolean isDataEntered()
    {
        for (JTextField textField : textFields)
        {
            if (textField.getText().trim().length() == 0)
                return false;
        }

        return true;
    }

    @Override
    public void insertUpdate(DocumentEvent e)
    {
        checkData();
    }

    @Override
    public void removeUpdate(DocumentEvent e)
    {
        checkData();
    }

    @Override
    public void changedUpdate(DocumentEvent e) {}

    private void checkData()
    {
        button.setEnabled( isDataEntered() );
    }

    private static void createAndShowUI()
    {
        JButton submit = new JButton( "Submit" );
        submit.setEnabled( false );

        JTextField textField1 = new JTextField(10);
        JTextField textField2 = new JTextField(10);

        DataEntered de = new DataEntered( submit );
        de.addTextField( textField1 );
        de.addTextField( textField2 );

        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(textField1, BorderLayout.WEST);
        frame.add(textField2, BorderLayout.EAST);
        frame.add(submit, BorderLayout.SOUTH);
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

also check if a 10 digit contact no is entered in one of the text field.

You would need to customize the isDataEntered() method to add a check for this additional requirement.

1
On

Validating a textfield is empty of not can be done by getting text from the textview and comparing it to ""

Suppose your TextField is textField.

if (textField.getText().trim().length>0) {
    //TextField is empty
} else {
    //TextField is not empty
}

Similarly if you want to see a 10 digit contact number.

if (textfield.getText().length == 10) {
    /*
     Here I'm not checking whether each character is a digit, 
     but you can do so by iterating through each character and checking
     whether it's a digit using isDigit() method
    */
} else {
    //Not 10 characters
} 

The appropriate listener in Java's swing to track changes in the text content of a JTextField is a DocumentListener, that you have to add to the document of the JTextField:

textField.getDocument().addDocumentListener(new DocumentListener() {
    // Enable the buttons here. 
});