21

ControlsFX class Dialogs is marked as deprecated.

What to use instead?

Dims
  • 47,675
  • 117
  • 331
  • 600

2 Answers2

16

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:

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"));
}
Amir
  • 8,821
  • 7
  • 44
  • 48
Mateus Viccari
  • 7,389
  • 14
  • 65
  • 101
  • Have you seen and tried this? "For users of JavaFX 8u60 and greater, download ControlsFX 8.40.10-SNAPSHOT builds." – mipa Sep 17 '15 at 07:15
  • The newer versions (at least a while ago) have Dialogs class deprecated, did they fix it on this version? I Can't find that version on maven, the last one is 8.40.9 on maven central. I could find the jar though, do i need to install it manually on my local repository? – Mateus Viccari Sep 17 '15 at 11:30
  • As the name of the file suggests it is a snapshot release and so it is unlikely that you will find it on maven central. So you will have to install it manually somehow. I don't know what the changes are but at least it seems to be necessary for 8u60 otherwise they would not mention it on their web site. – mipa Sep 17 '15 at 17:45
15

This blog post explains it all:

http://fxexperience.com/2014/09/announcing-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).

Community
  • 1
  • 1
Boomah
  • 1,172
  • 13
  • 24
  • 3
    Thanks for the link. Really a bad decision of controlsfx. The new lib is GPL and cannot be used in most applications. These two APIs therefor have a different focus and the old API should really not have been deprecated... – Marc von Renteln Nov 02 '14 at 06:42
  • 2
    According to https://bitbucket.org/controlsfx/openjfx-dialogs the License is GPL plus Classpath-Exception which means it CAN be used in commercial applications. (disclaimer: IANAL) – rli Nov 14 '14 at 06:46
  • @ArtjomB. Yeah, but your comment was over 3 months before the edit to make it a viable answer. Ah well, sorted now, answer is fine which is the ultimate goal – James Jun 22 '15 at 12:57