0

I am using JavaFx for creating a Java Standalone Application. I have seen some examples but I am not able to understand how to use the javaFX Task in my code scenario.

This is the Controller function which I am calling for Button onAction which I have set from SceneBuilder -->

public class MainScreenController {
    @FXML
    private JFXButton btnSelectImg;
    @FXML
    private ImageView imageViewObj;
    @FXML
    private ProgressBar progressBarObj;
//..
//..
    @FXML
    private void onFileSelectButtonClick() { 
        //Some Operations are carried out 
        //..
        //Then I want to set Image in ImageView
        imageViewObj.setImage(myImage);

        // Some Code Here
        //..

        // Set Progress
        progressBarObj.setProgress(0.1);

        // Some Code Here 
        //..

        // Set Progress
        progressBarObj.setProgress(0.2);

        //...
        //...

        // Maybe change some other Controls 

        //..........
    }
   //..
//..
}

Now here I am updating multiple controls in the same function gradually as the code progresses step by step but it gets updated at last when execution is done.

I want to update the controls while execution as shown in the code.

Vishal Nair
  • 2,141
  • 3
  • 26
  • 39

1 Answers1

2

This is a likely a duplicate of bits of other questions:

And perhaps some other questions.


As an overall approach, you define a Task, then within the execution body of the Task, you make use of Platform.runLater(), updateProgress() and other mechanisms to achieve what you need. See the related question for further explanations of these mechanisms.

final ImageView imageViewObj = new ImageView();
Task<Void> task = new Task<Void>() {
    @Override protected Void call() throws Exception {
        //Some Operations are carried out
        //..

        //Then I want to set Image in ImageView
        // use Platform.runLater()
        Platform.runLater(() -> imageViewObj.setImage(myImage));

        // Some Code Here
        //..

        // Set Progress
        updateProgress(0.1, 1);

        // Some Code Here
        //..

        // Set Progress
        updateProgress(0.2, 1);

        int variable = 2;
        final int immutable = variable;

        // Maybe change some other Controls
        // run whatever block that updates the controls within a Platform.runLater block.
        Platform.runLater(() -> {
            // execute the control update logic here...
            // be careful of updating control state based upon mutable data in the task thread.
            // instead only use immutable data within the runLater block (avoids race conditions).
        });

        variable++;

        // some more logic related to the changing variable.

        return null;
    }
};

ProgressBar updProg = new ProgressBar();
updProg.progressProperty().bind(task.progressProperty());

Thread thread = new Thread(task, "my-important-stuff-thread");
thread.setDaemon(true);
thread.start();
Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406