ARTICLE AD BOX
How do I make sure my custom Alert button is placed alongsode the cancel button? As of now, the cancel button is shoved to the right, while my custom button is put in a weird place that is neither left, right, nor center. I want all of them be right-aligned in this order: first my custom button, then the cancel button.
ButtonData.RIGHT puts the custom button in the cancel button's place. Not acceptable.
The custom button is semantically not a cancel button, so setting its ButtonData to CANCEL_CLOSE is not acceptable either.
More broadly, I want to avoid attaching any wrong semantics to the custom button just for the sake of alignment (e.g. OK_DONE). OTHER is as good as it gets for my case.
MRE:
import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.scene.control.Button; import javafx.scene.control.ButtonBar; import javafx.scene.control.ButtonType; import javafx.scene.layout.VBox; import javafx.stage.Stage; import java.util.Locale; import java.util.Optional; import java.util.function.Predicate; public class FXAlertDemo extends Application { static final ButtonType REPEAT = new ButtonType("Repeat", ButtonBar.ButtonData.OTHER); public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) throws Exception { Parent root = createRoot(); Scene scene = new Scene(root); primaryStage.setScene(scene); primaryStage.show(); } private Parent createRoot() { VBox root = new VBox(); root.paddingProperty().set(new Insets(5)); root.getChildren().setAll(createButton()); return root; } private Button createButton() { Button button = new Button(); button.textProperty().set("Perform Flaky Operation"); button.setOnAction(e -> performFlakyOperation()); return button; } private void performFlakyOperation() { Alert alert = new Alert(Alert.AlertType.CONFIRMATION); alert.setHeaderText("There was an error performing the operation."); alert.setContentText("Do you want to repeat?"); alert.getButtonTypes().setAll(REPEAT, ButtonType.CANCEL); Optional<ButtonType> buttonTypeOptional = alert.showAndWait(); buttonTypeOptional.filter(Predicate.isEqual(REPEAT)).ifPresent(b -> performFlakyOperation()); } }Java 8.
Explore related questions
See similar questions with these tags.

