Share the post "How To Create Alert Dialog Like JOptionPane In Java FX 2"
As of this writing, the answer is no. It is weird that the Java FX team did not include classes that developers can easily call to display message alerts like those in the JOptionPane class.
The workaround? Create your own. Well, the Java FX team did release a JAR file of a few classes, alert dialogs included but these look horrible. So the only solution was for me to create my own.
This is what I came up with.
I know, it looks simple. But you can easily make it look better using CSS. I only provided the basic for you to get started.
Here are the codes.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
public class AlertDialog extends Stage { private final int WIDTH_DEFAULT = 300; public static final int ICON_INFO = 0; public static final int ICON_ERROR = 1; public AlertDialog(Stage owner, String msg, int type) { setResizable(false); initModality(Modality.APPLICATION_MODAL); initStyle(StageStyle.TRANSPARENT); Label label = new Label(msg); label.setWrapText(true); label.setGraphicTextGap(20); label.setGraphic(new ImageView(getImage(type))); Button button = new Button("OK"); button.setOnAction(new EventHandler(){ @Override public void handle(ActionEvent arg0) { AlertDialog.this.close(); } }); BorderPane borderPane = new BorderPane(); borderPane.getStylesheets().add(getClass().getResource("alert.css").toExternalForm()); borderPane.setTop(label); HBox hbox2 = new HBox(); hbox2.setAlignment(Pos.CENTER); hbox2.getChildren().add(button); borderPane.setBottom(hbox2); // calculate width of string final Text text = new Text(msg); text.snapshot(null, null); // + 20 because there is padding 10 left and right int width = (int) text.getLayoutBounds().getWidth() + 40; if (width < WIDTH_DEFAULT) width = WIDTH_DEFAULT; int height = 100; final Scene scene = new Scene(borderPane, width, height); scene.setFill(Color.TRANSPARENT); setScene(scene); // make sure this stage is centered on top of its owner setX(owner.getX() + (owner.getWidth() / 2 - width / 2)); setY(owner.getY() + (owner.getHeight() / 2 - height / 2)); } private Image getImage(int type) { if (type == ICON_ERROR) return new Image(getClass().getResourceAsStream("/images/error.png")); else return new Image(getClass().getResourceAsStream("/images/info.png")); } } |
And this is the css file.
|
1 2 3 4 5 |
.root { -fx-background-color: #c0c0c0; -fx-background-radius: 5; -fx-padding: 10; } |
To use it, just call it like this:
|
1 |
new AlertDialog(stage, "Save Sucessful!", AlertDialog.ICON_INFO).showAndWait(); |
If you check the class, there is a method called getImage(). You will have to find your own error and info icons though ;).
The alert dialog will have a height of 100 pixels. If you put a long text, there is no need to worry because the alert dialog will expand in width while maintaining a max of 2 lines ;).
Lastly, the last few lines of the constructor are needed so that the alert dialog will always stay at the center on top of its parent Stage.