Share the post "How To Create Confirm Dialog Window In Java FX 2"
Here is my version of a confirm dialog window that shows a Yes and No button. The class also includes a key listener to close the window in case the user presses the ESC key. This is equivalent to pressing the No button.
This is how it looks like.
|
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
public class AlertPromptDialog extends Stage { private static final int WIDTH_DEFAULT = 400; private static Label label; private static AlertPromptDialog popup; private static int result; private static Image img; public static final int NO = 0; public static final int YES = 1; private AlertPromptDialog() { setResizable(false); initModality(Modality.APPLICATION_MODAL); initStyle(StageStyle.TRANSPARENT); img = new Image(AlertPromptDialog.class.getResourceAsStream("/images/MYICON.png")); addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler() { @Override public void handle(KeyEvent ke) { if (ke.getCode() == KeyCode.ESCAPE) { AlertPromptDialog.this.close(); } } }); label = new Label(); label.setWrapText(true); label.setGraphicTextGap(20); Button ybutton = new Button("Yes"); ybutton.setOnAction(new EventHandler(){ @Override public void handle(ActionEvent arg0) { result = YES; AlertPromptDialog.this.close(); } }); Button nbutton = new Button("No"); nbutton.setOnAction(new EventHandler(){ @Override public void handle(ActionEvent arg0) { result = NO; AlertPromptDialog.this.close(); } }); BorderPane borderPane = new BorderPane(); BorderPane dropShadowPane = new BorderPane(); dropShadowPane.getStyleClass().add("content"); dropShadowPane.setTop(label); HBox hbox = new HBox(); hbox.setSpacing(15); hbox.setAlignment(Pos.CENTER); hbox.getChildren().add(ybutton); hbox.setAlignment(Pos.CENTER); hbox.getChildren().add(nbutton); dropShadowPane.setBottom(hbox); borderPane.setCenter(dropShadowPane); Scene scene = new Scene(borderPane); scene.getStylesheets().add(getResource("alert.css").toExternalForm()); scene.setFill(Color.TRANSPARENT); setScene(scene); } public static int show(Stage owner, String msg) { if (popup == null) { popup = new AlertPromptDialog(); } label.setText(msg); label.setGraphic(new ImageView(img)); // calculate width of string final Text text = new Text(msg); text.snapshot(null, null); // + 40 because there is padding 10 left and right and there are 2 containers now // + 20 because there is text gap between icon and messge int width = (int) text.getLayoutBounds().getWidth() + 60; if (width + img.getWidth() < WIDTH_DEFAULT) width = WIDTH_DEFAULT; int height = 120; popup.setWidth(width); popup.setHeight(height); // make sure this stage is centered on top of its owner popup.setX(owner.getX() + (owner.getWidth() / 2 - popup.getWidth() / 2)); popup.setY(owner.getY() + (owner.getHeight() / 2 - popup.getHeight() / 2)); popup.showAndWait(); return result; } } |
And this is the content of the CSS file.
|
1 2 3 4 5 6 7 8 9 10 11 |
.root { -fx-effect: dropshadow(gaussian, rgba(0, 0, 0, 0.4), 10, 0.5, 0.0, 0.0); -fx-background-radius: 5; -fx-padding: 10; } .content { -fx-background-color: #c0c0c0; -fx-background-radius: 5; -fx-padding: 10; } |