Please help me with the following piece of code?(JAVA) -


this question has answer here:

what trying do:

i trying make basic timer in java.

what approach following:

i using thread.sleep(1000) make main thread sleep 1 second , thread awakes seconds field in code increments 1 , there normal mathematics minutes , hours.

the code itself:

public class extends javax.swing.jframe {      javax.swing.jtextfield text,minute,starter,hours;      a() {         super("timer!");          try{              javax.swing.uimanager.setlookandfeel(javax.swing.uimanager.getsystemlookandfeelclassname());          } catch(exception e) {          } {               this.setlayout(new java.awt.flowlayout(java.awt.flowlayout.center,20,20));                  text = new javax.swing.jtextfield();             hours = new javax.swing.jtextfield();             hours.setpreferredsize(new java.awt.dimension(25,25));             this.add(hours);              minute = new javax.swing.jtextfield();             minute.setpreferredsize(new java.awt.dimension(25,25));              javax.swing.jbutton starter = new javax.swing.jbutton("start!");             this.add(minute);              text.setpreferredsize(new java.awt.dimension(25,25));             this.add(text);                 starter.addmouselistener(new java.awt.event.mouseadapter() {                  @override                 public void mousereleased(java.awt.event.mouseevent e) {                     timer();                 }              });              this.add(starter);             this.pack();             this.setlocationrelativeto(null);             this.setvisible(true);             text.seteditable(false);              text.settext("0");             minute.seteditable(false);             hours.seteditable(false);                 minute.settext("0");                                   hours.settext("0");          }     }      private void timer() {          while(true) {              try {                      thread.sleep(1000);                  int = integer.parseint(text.gettext());                 i++;                 text.settext(string.valueof(i%60));                  if(i==60) {                      = integer.parseint(minute.gettext());                     i++;                           minute.settext(string.valueof(i));                      if(i==60) {                          minute.settext("0");                         int number = integer.parseint(hours.gettext());                         hours.settext(string.valueof(++number));                      }                                              }             } catch(exception e) {                 text.settext("crap!");             }         }        }      public static void main(string args[]){         o = new a();     }  } 

my problem

my problem or question when had not used jbutton starter , calling timer() method inside constructor, timer worked charm, when using jbutton , have put timer() call in event listener start timer per requirement application freezes , nothing happens.

like mentioned above, if call timer inside constructor , remove jbutton application works fine. when using jbutton (in order manually start timer), app freezes.

edit:

i sorry! forgot mention when app freezes freezes upon clicking jbutton.

edit:

this question not duplicate of other question question how create timer in java, never asked that! know that, asked in question mistake making in code, 1 of answers explains well. never asked question "how create timer in java".

you're not calling sleep() in main thread. you're doing in event dispatch thread. thread 1 handling ui events , repainting ui components when needed. since you're forcing sleep forever, can't job anymore.

use javax.swing.timer.

note works accident when timer in constructor: you're calling constructor main thread, , not blocking edt. violates swing threading rules: swing components must created , accessed edt. read the tutorial concurrency in swing.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -