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);
}
}
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:
You would need to customize the isDataEntered() method to add a check for this additional requirement.