Maybe too late, it can help someone too. For me, I used the JavaFX service and the task to create a splash screen as a preloader in a stand-alone JavaFX application. This is because the context of my project.
Create Anchor Panel and Progress Panel
@FXML private AnchorPane anchorPane; private MaskerPane progressPane; public static void main(String[] args) { launch(args); } @Override public void init() throws Exception { progressPane = new MaskerPane(); progressPane.setText(bundle.getString("root.pleaseWait")); progressPane.setVisible(false); AnchorPane.setLeftAnchor(progressPane, 0.0); AnchorPane.setTopAnchor(progressPane, 0.0); AnchorPane.setRightAnchor(progressPane, 0.0); AnchorPane.setBottomAnchor(progressPane, 0.0); anchorPane.getChildren().add(progressPane); } @Override public void start(Stage initStage) {
Create a screensaver service like this:
private final Service<Void> splashService = new Service<Void>() { @Override protected Task<Void> createTask() { return new Task<Void>() { @Override public Void call() throws Exception {
Start the service and show / hide the progress panel on initRoot when loading the main screen:
public void initRoot() { try { //.... splashService.restart(); //On succeeded, do this splashService.setOnRunning(new EventHandler<WorkerStateEvent>() { @Override public void handle(WorkerStateEvent event) { //Show mask on succeed showMask(Boolean.TRUE); } }); splashService.setOnSucceeded(new EventHandler<WorkerStateEvent>() { @Override public void handle(WorkerStateEvent event) { splashService.cancel(); //Hide mask on succeed showMask(Boolean.FALSE); } }); //..... primaryStage.show(); } catch (IOException ex) { ex.printStackTrace(); } }
To show / hide progress ...
showMask(boolean value){ progressPane.setVisible(value); };
Anatole ABE
source share