Alert a = new Alert(AlertType.ERROR); a.setTitle("Title of alert"); a.initStyle(StageStyle.UNDECORATED); a.setContentText("details of message"); a.showAndWait();
public static void showError(String title, String message) { final 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 showWarning(String title, String message) { final 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 showInformation(String title, String message) { final Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.initStyle(StageStyle.UNDECORATED); alert.setTitle("Information"); alert.setHeaderText(title); alert.setContentText(message); alert.showAndWait(); }
public static String showConfirm(String title, String message, String... options) { final 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(); } }); if (options == null || options.length == 0) { options = new String[] {OK, CANCEL}; } final List<ButtonType> buttons = new ArrayList<>(); for (final String option : options) { buttons.add(new ButtonType(option)); } alert.getButtonTypes().setAll(buttons); final Optional<ButtonType> result = alert.showAndWait(); if (!result.isPresent()) { return CANCEL; } else { return result.get().getText(); } }
public static void showException(String title, String message, Exception exception) { final Alert alert = new Alert(Alert.AlertType.ERROR); alert.initStyle(StageStyle.UTILITY); alert.setTitle("Exception"); alert.setHeaderText(title); alert.setContentText(message); final StringWriter sw = new StringWriter(); final PrintWriter pw = new PrintWriter(sw); exception.printStackTrace(pw); final String exceptionText = sw.toString(); final Label label = new Label("Details:"); final 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); final 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(); }