create JavaFX Progress Dialog - Java JavaFX

Java examples for JavaFX:Dialog

Description

create JavaFX Progress Dialog

Demo Code


//package com.java2s;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Service;
import javafx.concurrent.Worker.State;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.ProgressIndicator;

import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class Main {
    /**/*from   ww  w .ja  va  2 s .c  o  m*/
     * 
     * @param service
     * @param owner
     * @return
     */
    public static Stage createProgressDialog(final Service<Void> service,
            Stage owner) {
        final Stage stage = new Stage();
        stage.initModality(Modality.WINDOW_MODAL);
        stage.initOwner(owner);
        final BorderPane root = new BorderPane();
        final ProgressIndicator indicator = new ProgressIndicator();
        // have the indicator display the progress of the service:
        indicator.progressProperty().bind(service.progressProperty());
        // hide the stage when the service stops running:
        service.stateProperty().addListener(new ChangeListener<State>() {
            @Override
            public void changed(
                    ObservableValue<? extends State> observable,
                    State oldValue, State newValue) {
                if (newValue == State.CANCELLED || newValue == State.FAILED
                        || newValue == State.SUCCEEDED) {
                    stage.hide();
                }
            }
        });
        // A cancel button:
        Button cancel = new Button("Cancel");
        cancel.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                service.cancel();
            }
        });
        root.setCenter(indicator);
        root.setBottom(cancel);
        Scene scene = new Scene(root, 200, 200);
        stage.setScene(scene);
        return stage;
    }
}

Related Tutorials