Java Swing Library, concerning Stopwatch -


i have created small gui designed use apache lang's stopwatch. when specific textbox focused (using .isfocusowner() ) stopwatch timer started , when user fills in textbox correct string , clicks confirm button actionperformed used stop timer , print time.

this happens 3 times within application. code below how planned accomplish this, timer seems malfunction upon reaching last textbox confirmation.

the label_____good visible if button pressed , correct string within textbox.

should trying utilize actionperformed within each individual textbox?

while(!completed){               if(text1.isfocusowner() && !labelemailgood.isvisible()){                                 if(!stopwatch.isstarted()){                      stopwatch.start();                 }else{                  }             }               if(text2.isfocusowner() && !labelbankgood.isvisible()){                 if(!stopwatch.isstarted()){                     stopwatch.start();                 }else{                  }                 return;             }              if(text3.isfocusowner() && !labelfacegood.isvisible()){                              if(!stopwatch.isstarted()){                     stopwatch.start();                 }else{                  }                 return;             }              if(labelemailgood.isvisible() && labelbankgood.isvisible() && labelfacegood.isvisible()){                 completed = true;             }else{              }          }   private jbutton makebuttonpass2(){          buttpass2.settext("confirm password");         buttpass2.setbounds(250, 190, 160, 30);         buttpass2.addactionlistener(new actionlistener() {             public void actionperformed(actionevent e1){                  string pass2 = getpass2();             //the password confirmation                 //if nothing in text box                 if(text2.gettext().equals("")){                     labelbankgood.setvisible(false);                     labelbankbad.setvisible(false);                 }                 //if correct password in text box                 else if(text2.gettext().equals(pass2)){                     labelbankgood.setvisible(true);                     labelbankbad.setvisible(false);                     stopwatch.stop();                     info2.addlogintime(float.valueof(stopwatch.tostring().substring(6, 11)));                     system.out.println(stopwatch.tostring().substring(4, 11));                     stopwatch.reset();                 }                 //if incorrect password in text box                 else if(!text2.gettext().equals(pass2)){                     labelbankbad.setvisible(true);                     info2.addunsuccessfullogtimes();                     labelbankgood.setvisible(false);                 }              }         });         return buttpass2;     }    

what gui looks like.

an excellent idea add focuslistener, solution looking similar posted below:

text1.addfocuslistener(new focuslistener() {              @override             public void focusgained(focusevent arg0) {                 stopwatch.reset();                 stopwatch.start();                 return;             }              @override             public void focuslost(focusevent arg0) {                 stopwatch.stop();                 return;             }         }); 


Comments