java - Validation of text fields and contact no text field -
i have jframe
consisting of text fields (10) , textarea. want validate text fields , see if not empty , check if 10 digit contact no entered in 1 of text field. after checking text fields, want enable submit button used submit dat database.
i used following code adding text area condition not working,gives error:- exception in thread "awt-eventqueue-0" java.lang.nullpointerexception
here code used 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); }
}
can me enabling button after validating text fields?
here general purpose class enable/disable button text added/removed group of text fields.
it adds documentlistener documenent of each text field. button ben enable when text has been entered 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( ); } public boolean isdataentered() { (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 10 digit contact no entered in 1 of text field.
you need customize isdataentered() method add check additional requirement.
Comments
Post a Comment