java - JavaFX - Concurrency and updating label -


i've got problem these piece of code. want make application, constatnly displays randomized values in labels long toggle button pressed. that's created, works window after few seconds lagging horrible. doing wrong?

here code of class, generate random values:

public class valuemaker{     private stringpropterty x, y, z;     private random generator;     private boolean isstarted = false;     private int randomizedx(){ return generator.nextint(10); }     private int randomizedy(){ return generator.nextint(10); }     private int randomizedz(){ return generator.nextint(10); }      public stringproperty xproperty(){ return x; }     public stringproperty yproperty(){ return y; }     public stringproperty zproperty() { return z; }      public valuemaker(){         x = new simplestringproperty("0");         y = new simplestringproperty("0");         z = new simplestringproperty("0");         generator = new random();     }        private void setvalues(){         platform.runlater(new runnable() {             @override             public void run() {                 x.set(string.valueof(randomizedx()));                 y.set(string.valueof(randomizedy()));                 z.set(string.valueof(randomizedz()));             }         });     }      public startrandomize(){         isstarted = true;         thread t = new thread(new runnable() {             @override             public void run() {                 while (isstarted){                     setvalues();                 }             }         });         t.start();     }      public stoprandomize(){         isstarted = false;     } } 

and here piece of controller code, calls these methods:

   @fxml    private void initialize(){             labelx.textproperty().bind(valuemaker.xproperty());             labely.textproperty().bind(valuemaker.yproperty());             labelz.textproperty().bind(valuemaker.zproperty());             startrandomizebutton.selectedproperty().addlistener(new changelistener<boolean>() {                     @override                     public void changed(observablevalue<? extends boolean> observable, boolean oldvalue, boolean newvalue) {                         if (newvalue==true){                             valuemaker.startrandomize();                         }                         else{                             valuemaker.stoprandomize();                         }                      }                 });             } 

you jamming javafx application thread calling platform.runlater continuously. best way achieve use animationtimer called in each frame while active.

animationtimer timer = new animationtimer() {          @override     public void handle(long now) {         x.set(string.valueof(randomizedx()));         y.set(string.valueof(randomizedy()));         z.set(string.valueof(randomizedz()));     } }; 

and call timer in example :

startrandomizebutton.selectedproperty().addlistener(new changelistener<boolean>() {        @override        public void changed(observablevalue<? extends boolean> observable, boolean oldvalue, boolean newvalue) {                // newvalue==true not required                if (newvalue){                    timer.start();                }                else{                    timer.stop();                }        }); } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -