controlsfx对话框被弃用了什么?

ControlsFX类Dialogs被标记为已弃用。

用什么代替?

这篇博文解释了这一切:

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

自从8.0.6于5月29日发布以来,这个版本一直在酝酿 – 所以基本上是四个月。 这对我们来说并不典型(我们通常会有更快的发布),但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包含的其他function(但在JavaFX本身中缺少)。 (这包括进度,字体选择器,命令链接,登录等对话框)。

现在使用java 8 update 60,即使使用旧的非弃用版本的controlsfx也行不通。 因此,解决方案是使用java 8 update 40中包含的javafx的本机对话框API(不需要第三方库)。 它们不像控件fx那样直接且function齐全。 但是为了更快的编码,你可以创建一个包装类,就像我做的那样:

 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 buttons = new ArrayList<>(); for (String option : options) { buttons.add(new ButtonType(option)); } alert.getButtonTypes().setAll(buttons); Optional 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 result = dialog.showAndWait(); if (result.isPresent()) { return result.get(); } else { return null; } } } 

要使用对话框:

 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")); }