Javafx service to update GUI objects status -


i'm trying update gui status based on time consuming task. when push on button, want button inactive , cursor change until job completed. i've come code works needed.

import javafx.application.application; import javafx.beans.binding.bindings; import javafx.concurrent.service; import javafx.concurrent.task; import javafx.event.actionevent; import javafx.event.eventhandler; import javafx.scene.cursor; import javafx.scene.scene; import javafx.scene.control.button; import javafx.scene.layout.flowpane; import javafx.stage.stage;  public class testwait2 extends application {      @override     public void start(stage primarystage) {         flowpane root = new flowpane();         primarystage.setscene(new scene(root));          myservice myservice = new myservice();         primarystage.getscene().getroot().cursorproperty()                 .bind(bindings.when(myservice.runningproperty())                         .then(cursor.wait).otherwise(cursor.default));          button startbutton = new button();         startbutton.settext("button");         startbutton.disableproperty().bind(myservice.runningproperty());         root.getchildren().add(startbutton);          startbutton.setonaction(new eventhandler<actionevent>() {              @override             public void handle(actionevent event) {                 myservice.start();             }         });          primarystage.show();     }      private class myservice extends service<void> {          @override         protected task<void> createtask() {             return new task<void>() {                 @override                 protected void call() throws exception {                     thread.sleep(5000);                     return null;                 }             };         }     }      public static void main(string[] args) {         launch(args);     } } 

when launch works great first time. problem if click on button second time error.

exception in thread "javafx application thread" java.lang.illegalstateexception: can start service in ready state. in state succeeded 

any thoughts on how around issue?

i'm running on java 8u5.

the problem code try call myservice.start(); when on succeeded state(since started once).

this according javadoc of service start method

  • starts service. service must in ready state succeed in * *this call.
  • this method should called on fx application thread.

to make code work, need call myservice.restart().

since planning use service on , on can following:

    startbutton.setonaction(new eventhandler<actionevent>() {              @override             public void handle(actionevent event) {                //replace line                // myservice.start();                //with                myservice.restart();              }         }); 

Comments

Popular posts from this blog

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

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -