public boolean showConfirmDialog(String title, String header, String content, AlertType alertType) { final Alert alert = new Alert(alertType); alert.setTitle(title); alert.setHeaderText(header); alert.setContentText(content); alert.getButtonTypes().clear(); alert.getButtonTypes().addAll(ButtonType.YES, ButtonType.NO); //Deactivate Defaultbehavior for yes-Button: Button yesButton = (Button) alert.getDialogPane().lookupButton( ButtonType.YES ); yesButton.setDefaultButton( false ); //Activate Defaultbehavior for no-Button: Button noButton = (Button) alert.getDialogPane().lookupButton( ButtonType.NO ); noButton.setDefaultButton( true ); final Optional<ButtonType> result = alert.showAndWait(); return result.get() == ButtonType.YES; }
public static Optional<ButtonType> showSaveAndQuitDialog(String documentName) { Alert alert = new Alert(Alert.AlertType.CONFIRMATION); alert.setHeaderText(MessageFormat.format(RESOURCE_BUNDLE.getString("SaveBeforeClosing"), documentName)); alert.setContentText(RESOURCE_BUNDLE.getString("WarnSaveBeforeClosing")); ButtonType save = new ButtonType(RESOURCE_BUNDLE.getString("Save"), ButtonBar.ButtonData.YES); ButtonType dontSave = new ButtonType(RESOURCE_BUNDLE.getString("DontSave"), ButtonBar.ButtonData.NO); alert.getButtonTypes().setAll(save, dontSave, ButtonType.CANCEL); return alert.showAndWait(); }
ButtonType buttonTypeOne = new ButtonType("Yes"); ButtonType buttonTypeCancel = new ButtonType("No", ButtonBar.ButtonData.CANCEL_CLOSE); alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeCancel);
/** * Shows a question dialog. * * @param message the message * @param defaultYes true if yes is the default button * @return true if yes, false if no */ public boolean showQuestionDialog(String message, boolean defaultYes) { Alert alert = new Alert(Alert.AlertType.CONFIRMATION); alert.setTitle(FxFxBundle.getString("QUESTION")); setAlertMessage(alert, message); alert.getButtonTypes().clear(); alert.getButtonTypes().addAll(ButtonType.YES, ButtonType.NO); Button noButton = (Button) alert.getDialogPane().lookupButton(ButtonType.NO); Button yesButton = (Button) alert.getDialogPane().lookupButton(ButtonType.YES); noButton.setDefaultButton(!defaultYes); yesButton.setDefaultButton(defaultYes); Optional<ButtonType> result = alert.showAndWait(); return result.isPresent() && result.get() == ButtonType.YES; }
alert.initModality( Modality.NONE ); ButtonType buttonTypeCancel = new ButtonType( "No", ButtonBar.ButtonData.CANCEL_CLOSE ); alert.getButtonTypes().setAll( buttonTypeOne, buttonTypeCancel );
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(); } }
@Override public Optional<ButtonType> showConfirmationDialog(final Window parent, final String titleKey, final String messageKey, final ButtonType... buttonTypes) { final Alert alert = new Alert(Alert.AlertType.CONFIRMATION, fxResources.getString(messageKey)); alert.initOwner(parent); alert.setTitle(fxResources.getString(titleKey)); alert.setHeaderText(null); // add custom button types if specified if (buttonTypes.length > 0) { alert.getButtonTypes().setAll(buttonTypes); } return alert.showAndWait(); }
alert.getButtonTypes().addAll(reconnect);