0

In my UI, I have a Label that displays information when the user performs some tasks. This is a Spring/Hibernate/JavaFx application and all operations are currently performed in the JavaFx thread and last up to a few seconds max (like loading/saving data from a database). I would like to have something looking like this:

In the initialize method of my controller class

taskLabel.textProperty().bind(dataService.taskProperty());

In the DataService class :

public void saveData(Data someData) {
    taskProperty.set("Saving data");
    databaseService.saveData(someData);
    taskProperty.set("Waiting");
}

public Data loadData() {
    Data result = null;
    taskProperty.set("Loading data");
    result = databaseService.loadData();
    taskProperty.set("Waiting");
    return result;
}

These tasks take a few seconds, but the Label taskLabel only shows Waiting. I tried to use Platform.runLater, but I'm pretty sure even if dataService and databaseService are Spring services, their operations are still performed in the JavaFx thread (i never specified otherwise).

What's the proper way of doing this ?

Thanks in advance for your answers.

  • you might be blocking the fx application thread (hard to tell from the snippet) make sure you understand how to handle concurrency (and its support) in fx, when stuck come back with a [mcve] please .. – kleopatra Jul 19 '21 at 11:00
  • @kleopatra It's complicated to provide minimal example since the structure uses the spring and hibernate frameworks. But I'll try anyway when I have access to my code (not at home at the moment). Thanks for your answer. – Nicolas Coral Jul 19 '21 at 12:07
  • You need to run your "long running tasks" on a background thread to prevent blocking the FX Application Thread (which will mean the UI can't be updated). See https://stackoverflow.com/questions/30249493 – James_D Jul 19 '21 at 12:26
  • For reference, here is [some sample code for task based db access](https://gist.github.com/jewelsea/4957967) which is related to the answer to [JavaFX - Background Thread for SQL Query](https://stackoverflow.com/questions/14878788/javafx-background-thread-for-sql-query). The sample code uses JDBC, but some of the general principles should be applicable to your Spring based solution. – jewelsea Jul 19 '21 at 17:46

0 Answers0