ProgressMonitor的JavaFX等价物

ProgressMonitor的JavaFX等价物,java,javafx,javafx-8,Java,Javafx,Javafx 8,我们在Swing中使用的ProgressMonitor类是否有一个JavaFX等效类?还是我必须推出自己的实现?我特别想要的功能是,如果后台任务的执行时间超过一定时间,则会弹出一个进度对话框。我已经成功实现了与ProgressMonitor相当的最小JavaFX。这些方法对应于基于Swing的版本。如果您熟悉该类,那么您可以使用该类作为临时替代 import javafx.beans.property.ObjectProperty; import javafx.collections.Obser

我们在Swing中使用的ProgressMonitor类是否有一个JavaFX等效类?还是我必须推出自己的实现?我特别想要的功能是,如果后台任务的执行时间超过一定时间,则会弹出一个进度对话框。

我已经成功实现了与ProgressMonitor相当的最小JavaFX。这些方法对应于基于Swing的版本。如果您熟悉该类,那么您可以使用该类作为临时替代

import javafx.beans.property.ObjectProperty;
import javafx.collections.ObservableList;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Window;

import javax.swing.UIManager;

public class FXProgressMonitor {

    public static final int DEFAULT_MILLIS_TO_DECIDE_POPUP = 500;
    public static final int DEFAULT_MILLIS_TO_POPUP = 2000;

    public static final int DEFAULT_MIN = 0;
    public static final int DEFAULT_MAX = 100;

    private static final int VBOX_SPACING = 10;
    private static final int PROGRESS_BAR_WIDTH = 300;

    private final Window window;
    private Dialog<ButtonType> dialog;
    private Label noteLabel;
    private ProgressBar myBar;

    private final String message;
    private String note;
    private boolean canceled;

    private final long T0;
    private int millisToDecideToPopup;
    private int millisToPopup;

    private int min;
    private int max;

    public FXProgressMonitor(Window window, String message, String note, int min, int max) {
        this.window = window;

        this.message = message;
        this.note = note;
        this.canceled = false;

        this.T0 = System.currentTimeMillis();
        this.millisToDecideToPopup = DEFAULT_MILLIS_TO_DECIDE_POPUP;
        this.millisToPopup = DEFAULT_MILLIS_TO_POPUP;

        this.min = min;
        this.max = max;
    }

    private double getProgress(int nv) {
        return (nv - this.min) / (double)(this.max - this.min);
    }

    /**
     * Indicate the progress of the operation being monitored. If the specified
     * value is &gt;= the maximum, the progress monitor is closed.
     *
     * @param nv an int specifying the current value, between the maximum and
     * minimum specified for this component
     * @see #setMinimum
     * @see #setMaximum
     * @see #close
     */
    public void setProgress(int nv) {
        if (nv >= this.max) {
            close();
        } else {
            if (this.myBar != null) {
                double progress = getProgress(nv);
                this.myBar.setProgress(progress);
            } else {
                long T = System.currentTimeMillis();
                long dT = (int)(T - this.T0);
                if (dT >= this.millisToDecideToPopup) {
                    int predictedCompletionTime;
                    if (nv > this.min) {
                        predictedCompletionTime = (int)(dT * (this.max - this.min) / (nv - this.min));
                    } else {
                        predictedCompletionTime = this.millisToPopup;
                    }
                    if (predictedCompletionTime >= this.millisToPopup) {
                        if (this.note != null) {
                            this.noteLabel = new Label(this.note);
                        }

                        double progress = getProgress(nv);

                        this.myBar = new ProgressBar(progress);
                        this.myBar.setPrefWidth(PROGRESS_BAR_WIDTH);

                        this.dialog = new Dialog<>();
                        this.dialog.initOwner(this.window);
                        this.dialog.initModality(Modality.NONE);

                        String dialogTitle = UIManager.getString("ProgressMonitor.progressText");
                        this.dialog.setTitle(dialogTitle);

                        ObjectProperty<ButtonType> resultProperty = this.dialog.resultProperty();
                        resultProperty.addListener((observable, oldValue, newValue) -> {
                            canceled = true;
                        });
                        this.dialog.setHeaderText(this.message);

                        VBox box = new VBox(VBOX_SPACING, this.noteLabel, this.myBar);

                        DialogPane dialogPane = this.dialog.getDialogPane();
                        dialogPane.setContent(box);

                        ObservableList<ButtonType> buttonTypes = dialogPane.getButtonTypes();
                        buttonTypes.add(ButtonType.CANCEL);

                        this.dialog.show();
                    }
                }
            }
        }
    }

    /**
     * Indicate that the operation is complete. This happens automatically when
     * the value set by setProgress is &gt;= max, but it may be called earlier
     * if the operation ends early.
     */
    public void close() {
        if (this.dialog != null) {
            this.dialog.close();
            this.dialog = null;
            this.myBar = null;
        }
    }

    /**
     * Returns the minimum value -- the lower end of the progress value.
     *
     * @return an int representing the minimum value
     * @see #setMinimum
     */
    public int getMinimum() {
        return this.min;
    }

    /**
     * Specifies the minimum value.
     *
     * @param min an int specifying the minimum value
     * @see #getMinimum
     */
    public void setMinimum(int min) {
        this.min = min;
    }

    /**
     * Returns the maximum value -- the higher end of the progress value.
     *
     * @return an int representing the maximum value
     * @see #setMaximum
     */
    public int getMaximum() {
        return this.max;
    }

    /**
     * Specifies the maximum value.
     *
     * @param max an int specifying the maximum value
     * @see #getMaximum
     */
    public void setMaximum(int max) {
        this.max = max;
    }

    /**
     * @return true if the user hits the Cancel button in the progress dialog.
     */
    public boolean isCanceled() {
        return this.canceled;
    }

    /**
     * Specifies the amount of time to wait before deciding whether or not to
     * popup a progress monitor.
     *
     * @param millisToDecideToPopup an int specifying the time to wait, in
     * milliseconds
     * @see #getMillisToDecideToPopup
     */
    public void setMillisToDecideToPopup(int millisToDecideToPopup) {
        this.millisToDecideToPopup = millisToDecideToPopup;
    }

    /**
     * Returns the amount of time this object waits before deciding whether or
     * not to popup a progress monitor.
     *
     * @see #setMillisToDecideToPopup
     */
    public int getMillisToDecideToPopup() {
        return this.millisToDecideToPopup;
    }

    /**
     * Specifies the amount of time it will take for the popup to appear.
     * (If the predicted time remaining is less than this time, the popup
     * won't be displayed.)
     *
     * @param millisToPopup  an int specifying the time in milliseconds
     * @see #getMillisToPopup
     */
    public void setMillisToPopup(int millisToPopup) {
        this.millisToPopup = millisToPopup;
    }

    /**
     * Returns the amount of time it will take for the popup to appear.
     *
     * @see #setMillisToPopup
     */
    public int getMillisToPopup() {
        return this.millisToPopup;
    }

    /**
     * Specifies the additional note that is displayed along with the
     * progress message. Used, for example, to show which file the
     * is currently being copied during a multiple-file copy.
     *
     * @param note  a String specifying the note to display
     * @see #getNote
     */
    public void setNote(String note) {
        this.note = note;
        if (this.noteLabel != null) {
            this.noteLabel.setText(note);
        }
    }

    /**
     * Returns the additional note that is displayed along with the
     * progress message.
     *
     * @return a String specifying the note to display
     * @see #setNote
     */
    public String getNote() {
        return this.note;
    }
}
导入javafx.beans.property.ObjectProperty;
导入javafx.collections.ObservableList;
导入javafx.scene.control.ButtonType;
导入javafx.scene.control.Dialog;
导入javafx.scene.control.DialogPane;
导入javafx.scene.control.Label;
导入javafx.scene.control.ProgressBar;
导入javafx.scene.layout.VBox;
导入javafx.stage.model;
导入javafx.stage.Window;
导入javax.swing.UIManager;
公共类监视器{
公共静态最终整数默认值为500;
公共静态最终整数默认值\u MILLIS\u至\u POPUP=2000;
公共静态最终整数默认值_MIN=0;
公共静态最终整数默认值_MAX=100;
专用静态最终int VBOX_间距=10;
专用静态最终整型进度条宽度=300;
私人最终窗口;
私人对话;
私人标签;
私人ProgressBar myBar;
私有最终字符串消息;
私人弦乐;
取消私有布尔运算;
私人最终长T0;
私有整数毫秒级;
私有int Millistopup;
私用int min;
私人int max;
公共FXProgressMonitor(窗口窗口、字符串消息、字符串注释、最小整数、最大整数){
this.window=窗口;
this.message=消息;
this.note=注释;
this.cancelled=false;
this.T0=System.currentTimeMillis();
this.millisToDecideToPopup=默认的要决定的弹出窗口;
this.millisToPopup=默认的\u MILLIS\u到\u弹出窗口;
this.min=min;
this.max=max;
}
私人双通道进度(int nv){
返回值(nv-this.min)/(double)(this.max-this.min);
}
/**
*指示正在监视的操作的进度。如果指定
*值为=最大值时,进度监视器关闭。
*
*@param nv指定当前值的int,介于最大值和
*为此组件指定的最小值
*@见#最小值
*@see#设置最大值
*@see#关闭
*/
公共空间设置进度(内华达州){
如果(nv>=此.max){
close();
}否则{
如果(this.myBar!=null){
双进度=获取进度(nv);
this.myBar.setProgress(progress);
}否则{
long T=System.currentTimeMillis();
长dT=(int)(T-this.T0);
如果(dT>=this.millisToDecideToPopup){
int预测完成时间;
如果(nv>this.min){
predictedCompletionTime=(int)(dT*(this.max-this.min)/(nv-this.min));
}否则{
predictedCompletionTime=this.millisToPopup;
}
如果(predictedCompletionTime>=此.millistopOp){
如果(this.note!=null){
this.noteLabel=新标签(this.note);
}
双进度=获取进度(nv);
this.myBar=新进度条(进度);
this.myBar.setPrefWidth(进度条宽度);
this.dialog=新建对话框();
this.dialog.initOwner(this.window);
this.dialog.initModality(Modality.NONE);
String dialogTitle=UIManager.getString(“ProgressMonitor.progressText”);
this.dialog.setTitle(dialogTitle);
ObjectProperty resultProperty=this.dialog.resultProperty();
resultProperty.addListener((可观察、旧值、新值)->{
取消=真;
});
this.dialog.setHeaderText(this.message);
VBox box=新的VBox(VBox\u间距,this.notelab,this.myBar);
DialogPane DialogPane=this.dialog.getDialogPane();
dialogPane.setContent(框);
ObservableList buttonTypes=dialogPane.getButtonTypes();
ButtonType.add(ButtonType.CANCEL);
this.dialog.show();
}
}
}
}
}
/**
*指示操作已完成。当
*setProgress设置的值为=max,但可以更早调用它
*如果手术提前结束。
*/
公众假期结束(){
如果(this.dialog!=null){
this.dialog.close();
this.dialog=null;
this.myBar=null;
}
}
/**
*返回最小值--进度值的下限。
*
*@返回表示最小值的整数
*@见#最小值
*/
public int getMinimum(){
返回这个.min;
}
/**
*指定最小值。
*
*@param min指定最小值的int
*@see#getMinimum
*/
公共无效设置最小值(整数最小值){
this.min=min;
}
/**
*返回最大值--进度值的上限。
*
*@返回表示最大值的整数
*@see#设置最大值
*/
公共整数getMaximum(){
返回此.max;
}
/**
*指定最大值。
*
*@param max指定th的整数