Need to add bind between component progress properties with the service.
progressBar.progressProperty().bind(service.progressProperty());
Here a complete working example.
import javafx.application.Application;
import javafx.beans.value.*;
import javafx.collections.*;
import javafx.concurrent.*;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class TaskTest extends Application {
public static void main(String[] args) throws Exception {
launch(args);
}
public void start(final Stage stage) throws Exception {
final Label statusLabel = new Label("Status");
final Button runButton = new Button("Run");
final ListView<String> peopleView = new ListView<String>();
peopleView.setPrefSize(220, 162);
final ProgressBar progressBar = new ProgressBar();
progressBar.prefWidthProperty().bind(peopleView.prefWidthProperty());
runButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
Service<ObservableList<String>> service = new Service() {
@Override
protected Task createTask() {
return new Task() {
@Override
protected ObservableList<String> call() throws Exception {
updateMessage("Finding friends . . .");
for (int i = 0; i < 10; i++) {
Thread.sleep(200);
updateProgress(i + 1, 10);
}
updateMessage("Finished.");
return FXCollections.observableArrayList("John", "Jim", "Geoff", "Jill", "Suki");
}
};
}
};
statusLabel.textProperty().bind(service.messageProperty());
runButton.disableProperty().bind(service.runningProperty());
peopleView.itemsProperty().bind(service.valueProperty());
progressBar.progressProperty().bind(service.progressProperty());
service.stateProperty().addListener(new ChangeListener<Worker.State>() {
@Override
public void changed(ObservableValue<? extends Worker.State> observableValue, Worker.State oldState, Worker.State newState) {
if (newState == Worker.State.SUCCEEDED) {
System.out.println("This is ok, this thread " + Thread.currentThread() + " is the JavaFX Application thread.");
runButton.setText("Voila!");
}
}
});
service.start();
}
});
final VBox layout
= VBoxBuilder.create().spacing(8).children(
VBoxBuilder.create().spacing(5).children(
HBoxBuilder.create().spacing(10).children(
runButton,
statusLabel).build(),
progressBar
).build(),
peopleView
).build();
layout.setStyle("-fx-background-color: cornsilk; -fx-padding:10; -fx-font-size: 16;");
Scene scene = new Scene(layout);
stage.setScene(scene);
stage.show();
}
}