In my application, I have to build big panes with a lot of content. I will show a ProgressIndicator while the GUI is loading.
My first test, I will show a ProgressIndicator while I adding a lot of tabs into a TabPane.
That's my test Code:
public class SampleController implements Initializable {
private TabPane tabPane;
@FXML
private BorderPane borderPane;
ProgressIndicator myProgressIndicator;
Task<Void> myLongTask;
@Override
public void initialize(URL location, ResourceBundle resources)
{
myProgressIndicator = new ProgressIndicator();
Pane p1 = new Pane(myProgressIndicator);
tabPane = new TabPane();
Pane p2 = new Pane(tabPane);
myLongTask = new Task<Void>()
{
@Override
protected Void call() throws Exception
{
for (int i = 1; i < 1000; i++)
{
// Thread.sleep(10);
Tab newTab = new Tab("Number:" + i);
tabPane.getTabs().add(newTab);
}
return null;
}
};
borderPane.centerProperty().bind(Bindings.when(myLongTask.runningProperty()).then(p1).otherwise(p2));
new Thread(myLongTask).start();
}
}
But the application will show the window if the Task has finished. If I replace the lines inside the for-loop with Thread.sleep(10)
the application show the Indicator and, after all, sleep, it shows the GUI.
How can I show an Indicator while the GUI is not loaded already?
You have a
Task
that creates a result (i.e. aTabPane
). Therefore it's more convenient to useTabPane
as type parameter instead ofVoid
also you should callupdateProgress
to update theprogress
property and bind that property to theprogress
property of theProgressIndicator
.The result can be added to the
BorderPane
in theonSucceded
handler instead of creating a (more or less) complicated binding:Simply creating the tabs is fast however, and you won't see any progress indicator in the UI. Layouting a
TabPane
with 999Tabs
however is rather slow. The UI will most likely freeze for a short time. You can work around this by adding only a limited number ofTab
s in each frame:Return a
List<Tab>
from the task instead of aTabPane
; theseTab
s should not be added to theTabPane
(yet). You can use aAnimationTimer
to add a fixed number of tabs each frame:I have change the class like this:
}
Is it this what you mean?