java - JFormatted Field yielding focus when it shouldn't -


the other day asked question, here, , received suitable answer. however, when tried convert sample code own program , begin morph needs, wasn't working way should.

i've gone through both sets of codes several times , real difference see code uses panel instead of box, however, haven't seen documentation describing differences in performance inputverifier. program should not allow lenient day does, , allows jformattedfield yield focus when shouldn't . problem?

my current code:

public class hello implements actionlistener{     private jframe frame = new jframe();     private jpanel panel = new jpanel();     private date endingdate = new date();     private string endingstring = null;     private simpledateformat format = new simpledateformat("mm/dd/yyyy");     private jformattedtextfield formattedfield = null;     private jlabel label1 = new jlabel();     private jlabel label2 = new jlabel();     private textarea ta = new textarea();     private button b = new button("click");      public hello() {         b.addactionlistener(this);         label1 = new jlabel("test");         label2 = new jlabel("test");          formattedfield = createformattedtextfield();         format.setlenient(false);          panel.add(formattedfield);         panel.add(label1);         panel.add(label2);         panel.add(ta);         panel.add(b);         frame.add(panel);         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.pack();         frame.setvisible(true);       }      public static void main(string[] args) {         swingutilities.invokelater(new runnable(){             public void run(){                 new hello();                 }         });          system.out.println("hello, world");     }      private jformattedtextfield createformattedtextfield() {         jformattedtextfield formattedfield = null;         try {             maskformatter datemask = new maskformatter("##/##/####");             formattedfield = new jformattedtextfield(datemask);         } catch (parseexception ex) {             logger.getlogger(hello.class.getname()).log(level.severe, null, ex);         }         formattedfield.setcolumns(10);         formattedfield.setinputverifier(getinputverifier());         return formattedfield;     }      private inputverifier getinputverifier() {         inputverifier verifier = new inputverifier() {              @override             public boolean verify(jcomponent input) {                 jformattedtextfield field = (jformattedtextfield) input;                 string text = field.gettext();                 return isvaliddate(text);             }              @override             public boolean shouldyieldfocus(jcomponent input) {                 boolean valid = verify(input);                 if (!valid) {                     joptionpane.showmessagedialog(null, "please enter valid date in format dd/mm/yyyy");                 }                 return valid;             }          };         return verifier;     }      public boolean isvaliddate(string datestring) {         try {             format.parse(datestring);             return true;         } catch (parseexception ex) {             return false;          }     }      public void actionperformed(actionevent e) {         system.out.println("action performed");         system.out.println(formattedfield);         endingdate = (date) formattedfield.getvalue();          system.out.println(endingdate);         endingstring = format.format(endingdate);         system.out.println(endingstring);      } } 

code answer previous question:

public class inputverifydate {      private simpledateformat format = new simpledateformat("mm/dd/yyyy");      public inputverifydate() {         jformattedtextfield formattedfield = createformattedtextfield();         jtextfield field = new jtextfield(10);         format.setlenient(false);          box box = box.createverticalbox();         box.add(formattedfield);         box.add(box.createverticalstrut(10));         box.add(field);         box.setborder(new emptyborder(10, 10, 10, 10));          jframe frame = new jframe();         frame.add(box);         frame.pack();         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.setlocationrelativeto(null);         frame.setvisible(true);     }      private jformattedtextfield createformattedtextfield() {         jformattedtextfield formattedfield = null;         try {             maskformatter datemask = new maskformatter("##/##/####");             formattedfield = new jformattedtextfield(datemask);         } catch (parseexception ex) {             logger.getlogger(inputverifydate.class.getname()).log(level.severe, null, ex);         }         formattedfield.setcolumns(10);         formattedfield.setinputverifier(getinputverifier());         return formattedfield;     }      private inputverifier getinputverifier() {         inputverifier verifier = new inputverifier() {              @override             public boolean verify(jcomponent input) {                 jformattedtextfield field = (jformattedtextfield) input;                 string text = field.gettext();                 return isvaliddate(text);             }              @override             public boolean shouldyieldfocus(jcomponent input) {                 boolean valid = verify(input);                 if (!valid) {                     joptionpane.showmessagedialog(null, "please enter valid date in format mm/dd/yyyy");                 }                 return valid;             }          };         return verifier;     }      public boolean isvaliddate(string datestring) {         try {             format.parse(datestring);             return true;         } catch (parseexception ex) {             return false;          }     }      public static void main(string[] args) {         swingutilities.invokelater(new runnable() {             public void run() {                 new inputverifydate();             }         });      } } 

after copying code 1 program other found problem. using entirely swing components except textarea, awt. if change swing jtextarea event fires expected. :)

i imagine caused awt layer (which understand primitive foundation swing) not understand swings' more advanced events.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -