Dims
Dims

Reputation: 51159

So how to display message box in JavaFX?

I have read this: http://code.makery.ch/blog/javafx-dialogs-official/

I don't think 40 lines of code is acceptable to display simple exception message dialog box.

So, how to display dialog boxes in JavaFX? May be ControlsFX can help?

UPDATE

Compare how it was done in Swing:

JOptionPane.showMessageDialog(frame, 
    "Eggs are not supposed to be green.",
    "Inane error",
    JOptionPane.ERROR_MESSAGE);

It is

ONE

LINE

OF

CODE

This is more than enough.

Upvotes: 0

Views: 20099

Answers (1)

explv
explv

Reputation: 2759

You just need to create a new Alert with its content set to a TextArea inside of a ScrollPane, and then add your exception text to the TextArea

Exception e = new Exception("An exception!!!!!!!!!!!!!!!!!");
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));

Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setHeaderText("An exception occurred!");
alert.getDialogPane().setExpandableContent(new ScrollPane(new TextArea(sw.toString())));
alert.showAndWait();

UPDATE to match OP's update:

The equivelant in JavaFX to your Swing example is:

new Alert(Alert.AlertType.ERROR, "This is an error!").showAndWait();

Upvotes: 12

Related Questions