java controlfx 对话框因什么而被弃用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26341152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:45:31  来源:igfitidea点击:

controlsfx Dialogs deprecated for what?

javajavafxjavafx-8controlsfx

提问by Dims

ControlsFX class Dialogsis marked as deprecated.

ControlsFX 类Dialogs被标记为已弃用。

What to use instead?

用什么代替?

采纳答案by Boomah

This blog post explains it all:

这篇博文解释了这一切:

http://fxexperience.com/2014/09/announcing-controlsfx-8-20-7/

http://fxexperience.com/2014/09/annoucing-controlsfx-8-20-7/

This release has been brewing since 8.0.6 was released on May 29th – so basically four months. This is not typical for us (we normally have much quicker releases), but Eugene and I were both distracted on a major undertaking – elevating the ControlsFX dialogs API and implementation into the next release of JavaFX itself (it'll be appearing in JavaFX 8u40, although the API is vastly different than what you see in ControlsFX 8.0.6). The end result is that we iterated through a bunch of API design work (see RT-12643) and none of that benefited ControlsFX, but it took up all our time.

Once JavaFX 8u40 dialogs were API complete (which was only mid-August), we developed a plan for how to proceed with ControlsFX dialogs. In essence we didn't feel that it was wise to maintain a dialogs API in ControlsFX that was so divergent than what will ship in JavaFX 8u40. Therefore, the plan that we developed was to deprecate the old ControlsFX API, fork the JavaFX dialogs API into a new project called openjfx-dialogs, and to recreate the additional features that ControlsFX includes (but are lacking in JavaFX itself) using the new API (this includes dialogs like progress, font chooser, command link, login, etc).

此版本自 5 月 29 日发布 8.0.6 以来一直在酝酿中 - 所以基本上是四个月。这对我们来说并不常见(我们通常发布更快的版本),但是 Eugene 和我都在一项重大任务上分心——将 ControlsFX 对话框 API 和实现提升到 JavaFX 本身的下一个版本(它将出现在 JavaFX 8u40 中) ,尽管 API 与您在 ControlsFX 8.0.6 中看到的有很大不同)。最终结果是我们迭代了一堆 API 设计工作(参见 RT-12643),但这些工作都没有使 ControlsFX 受益,但它占用了我们所有的时间。

JavaFX 8u40 对话框完成 API 后(仅在 8 月中旬),我们制定了如何继续使用 ControlsFX 对话框的计划。从本质上讲,我们认为在 ControlsFX 中维护一个与 JavaFX 8u40 中将提供的内容截然不同的对话框 API 是不明智的。因此,我们制定的计划是弃用旧的 ControlsFX API,将 JavaFX 对话框 API 分叉到一个名为 openjfx-dialogs 的新项目中,并使用新 API 重新创建 ControlsFX 包含(但 JavaFX 本身缺乏)的附加功能(这包括进度、字体选择器、命令链接、登录等对话框)。

回答by Mateus Viccari

Now with java 8 update 60, even using old non-deprecated version of controlsfx doesn't work. So, the solution is using the native dialog API from javafx included in java 8 update 40 (doesn't need 3rd party libs). They aren't as straight forward and full of features as controls fx. But for faster coding, you could create a wrapper class, like this one I made:

现在使用 java 8 update 60,即使使用旧的未弃用的 controlfx 版本也不起作用。因此,解决方案是使用 java 8 update 40 中包含的 javafx 的本机对话 API(不需要 3rd 方库)。它们不像控件 fx 那样直接且功能齐全。但是为了更快的编码,您可以创建一个包装类,就像我制作的这个类:

package br.atualy.devolucaodevenda.util;

import javafx.scene.control.*;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.StageStyle;

import java.awt.*;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class FxDialogs {

    public static void showInformation(String title, String message) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.initStyle(StageStyle.UTILITY);
        alert.setTitle("Information");
        alert.setHeaderText(title);
        alert.setContentText(message);

        alert.showAndWait();
    }

    public static void showWarning(String title, String message) {
        Alert alert = new Alert(Alert.AlertType.WARNING);
        alert.initStyle(StageStyle.UTILITY);
        alert.setTitle("Warning");
        alert.setHeaderText(title);
        alert.setContentText(message);

        alert.showAndWait();
    }

    public static void showError(String title, String message) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.initStyle(StageStyle.UTILITY);
        alert.setTitle("Error");
        alert.setHeaderText(title);
        alert.setContentText(message);

        alert.showAndWait();
    }

    public static void showException(String title, String message, Exception exception) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.initStyle(StageStyle.UTILITY);
        alert.setTitle("Exception");
        alert.setHeaderText(title);
        alert.setContentText(message);

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        exception.printStackTrace(pw);
        String exceptionText = sw.toString();

        Label label = new Label("Details:");

        TextArea textArea = new TextArea(exceptionText);
        textArea.setEditable(false);
        textArea.setWrapText(true);

        textArea.setMaxWidth(Double.MAX_VALUE);
        textArea.setMaxHeight(Double.MAX_VALUE);
        GridPane.setVgrow(textArea, Priority.ALWAYS);
        GridPane.setHgrow(textArea, Priority.ALWAYS);

        GridPane expContent = new GridPane();
        expContent.setMaxWidth(Double.MAX_VALUE);
        expContent.add(label, 0, 0);
        expContent.add(textArea, 0, 1);

        alert.getDialogPane().setExpandableContent(expContent);

        alert.showAndWait();
    }

    public static final String YES = "Yes";
    public static final String NO = "No";
    public static final String OK = "OK";
    public static final String CANCEL = "Cancel";

    public static String showConfirm(String title, String message, String... options) {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.initStyle(StageStyle.UTILITY);
        alert.setTitle("Choose an option");
        alert.setHeaderText(title);
        alert.setContentText(message);

        //To make enter key press the actual focused button, not the first one. Just like pressing "space".
        alert.getDialogPane().addEventFilter(KeyEvent.KEY_PRESSED, event -> {
            if (event.getCode().equals(KeyCode.ENTER)) {
                event.consume();
                try {
                    Robot r = new Robot();
                    r.keyPress(java.awt.event.KeyEvent.VK_SPACE);
                    r.keyRelease(java.awt.event.KeyEvent.VK_SPACE);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        if (options == null || options.length == 0) {
            options = new String[]{OK, CANCEL};
        }

        List<ButtonType> buttons = new ArrayList<>();
        for (String option : options) {
            buttons.add(new ButtonType(option));
        }

        alert.getButtonTypes().setAll(buttons);

        Optional<ButtonType> result = alert.showAndWait();
        if (!result.isPresent()) {
            return CANCEL;
        } else {
            return result.get().getText();
        }
    }

    public static String showTextInput(String title, String message, String defaultValue) {
        TextInputDialog dialog = new TextInputDialog(defaultValue);
        dialog.initStyle(StageStyle.UTILITY);
        dialog.setTitle("Input");
        dialog.setHeaderText(title);
        dialog.setContentText(message);

        Optional<String> result = dialog.showAndWait();
        if (result.isPresent()) {
            return result.get();
        } else {
            return null;
        }

    }

}

To use the dialogs:

要使用对话框:

FxDialogs.showInformation("Hi", "Good Morning y'all!");
if (FxDialogs.showConfirm("Choose one baby!", "Can i ask you a question?", FxDialogs.YES, FxDialogs.NO).equals(FxDialogs.YES)) {
    FxDialogs.showWarning(null, "Pay attention to my next question!");
    String answer = FxDialogs.showTextInput("Are you a pink elephant disguised as a flying pig?", "Tell me!", "No");
    FxDialogs.showError(null, "You should not have said " + answer + "!");
    FxDialogs.showException("Now i'm angry", "I'm going home...", new RuntimeException("Exception caused by angry dinossaurs"));
}