swing - How to update the label box every 2 seconds in java fx? -


i'm trying simulate basic thermostat in application gui.

i want update label box value every 2 secs new temperature value.

for example, intial temperature displayed 68 degrees , updated 69, 70, etc. till 75 every 2 seconds.

this piece of code wrote in java fx. controlpanel object of te form label box present. updates final value 75. doesnt update every 2 secs. have written method pause cause 2 secs delay. labels updated final values not updated every 2 secs. when debug, can see values increased 1 every 2 secs. code written in button onclick event

private void jbutton1actionperformed(java.awt.event.actionevent evt) {                                              int i=0;     timer asd = new timer(1000,null);      asd.setdelay(1000);      while(i < 10)     {          jtextfield1.settext(integer.tostring(i));          i++;           asd.start();     }  }   

to solve task using timer need implement timertask code , use timer#scheduleatfixedrate method run code repeatedly:

timer timer = new timer();     timer.scheduleatfixedrate(new timertask() {         @override         public void run() {             system.out.print("i called every 2 seconds");         }     }, 0, 2000); 

also note calling ui operations must done on swing ui thread (or fx ui thread if using javafx):

private int = 0; private void jbutton1actionperformed(java.awt.event.actionevent evt) {     timer timer = new timer();     timer.scheduleatfixedrate(new timertask() {         @override         public void run() {             swingutilities.invokelater(new runnable() {                 @override                 public void run() {                     jtextfield1.settext(integer.tostring(i++));                 }             });         }     }, 0, 2000); } 

in case of javafx need update fx controls on "fx ui thread" instead of swing one. achieve use javafx.application.platform#runlater method instead of swingutilities


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -