Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

java - Validation of text fields and contact no text field

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

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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. 
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...