I seem to be struggling with some UI concepts.
I have a ListView that I'm populating with Task. I understand that fairly well.
Once the ListView is populated, each item represents a new long running Task. When the user kicks off those subsequent tasks, I want to provide feedback on progress as the application loops through each. I thought the best way to do this is would be with an HBox. Each ListView row consists of an HBox, which in turn contains a ProgressIndicator and the name of the long running task. So in my first long Running task used to populate the ListView, I do something roughly like this:
updateMessage("Start");
items = FXCollections.observableArrayList();
For Loop
{
HBox hbox = new HBox();
hbox.setAlignment(Pos.CENTER_LEFT);
hbox.setSpacing(10);
ProgressIndicator pi = new ProgressIndicator();
pi.setVisible(false);
pi.setProgress(0);
Text t = new Text("MyLabel");
hbox.getChildren().addAll(pi, t );
items.add(hbox);
}
updateMessage("Done");
What I'm unsure of is how to access the ProgressIndicator in the main thread in order to bind it to the second series of long running tasks. I was thinking I could loop through the ListView, then query the HBox in each item for the ProgressIndicator. But that doesn't seem possible.
I ended up creating a separate List of ProgressIndicators in the main thread that I pass into the long running Tasks. Now my Task looks something like this:
public class MyTask extends Task<ObservableList<HBox>>
{
private List<ProgressIndicator> progress
public MyTask (List<ProgressIndicator> progress
{
this.progress = progress;
}
@Override
public ObservableList<HBox> call()
{
ObservableList<HBox> items = null;
updateMessage("Start...");
// For loop here to loop through all items
// for(int i=0; i < items; i++
HBox hbox = new HBox();
hbox.setAlignment(Pos.CENTER_LEFT);
hbox.setSpacing(10);
Text t = new Text("My Label");
hbox.getChildren().addAll(progress.get(i).pi, t );
items.add(hbox);
updateMessage("Done " );
return items;
}
}
Then when I kick of the secondary long running tasks, I bind the correct ProgressIndicator in the list to the Task (s)
progress.get(0).pi.progressProperty().bind(myNewTask.progressProperty());
I feel I'm missing something and there is a better way?