-1

I am trying to pop-up the box in the center of the application when I resize the application or move it. I tried with css alignment and also using Java. Is it possible if I don't use pane and directly add box in the scene?

Here is my code:

public Boolean call(String question) {
  final Stage dialogStage = new Stage(StageStyle.UNDECORATED);
  dialogStage.initModality(Modality.WINDOW_MODAL);
  dialogStage.initOwner(owner);
  dialogStage.setTitle("ConfirmTitle"); // WIP, waiting for the strings&trans
  final Button ok = new Button(
      nmsGuiContainer.getI18nService().getMessage("com.mlnms.gui.fmwk.main.container.ok")); // WIP,
                                                                                            // waiting
                                                                                            // for
                                                                                            // the
  // strings&trans
  ok.getStyleClass().add(HTML_POPUP_BUTTON_STYLE);
  final Button cancel = new Button(
      nmsGuiContainer.getI18nService().getMessage("com.mlnms.gui.fmwk.main.container.cancel")); // WIP,
                                                                                                // waiting
  // for the
  // strings&trans
  cancel.getStyleClass().add(HTML_POPUP_BUTTON_STYLE);
  final Text text = new Text(question);
  text.getStyleClass().add(HTML_POPUP_STYLE);
  final Insets ins = new Insets(10);
  final VBox box = new VBox();
  box.setAlignment(Pos.BOTTOM_CENTER);
  box.setSpacing(10);
  box.setPadding(ins);
  final HBox buttons = new HBox(10);
  buttons.getChildren().addAll(ok, cancel);
  buttons.setAlignment(Pos.CENTER);
  buttons.setPadding(ins);
  box.getChildren().addAll(text, buttons);
  box.getStyleClass().add(HTML_POPUP_STYLE);
  StackPane pane = new StackPane();
  pane.setAlignment(box, Pos.CENTER);
  pane.getChildren().add(box);
  Scene scene = new Scene(pane);
  try {
    URL javafxCss = nmsGuiContainer.getBundleContext().getBundle()
        .getResource(NmsGuiContainer.JAVAFX_CSS_URL);
    scene.getStylesheets().add(javafxCss.toExternalForm());

  } catch (Exception e) {
    LOGGER.error("Cannot load the CSS file for JavaFX components ", e);
  }
  dialogStage.setScene(scene);
  ok.setCancelButton(false);

  final boolean[] res = new boolean[1];
  ok.setOnAction(new CloseDialogHandler(dialogStage, res));
  cancel.setCancelButton(true);
  cancel.setOnAction(new CloseDialogHandler(dialogStage, null));
  dialogStage.centerOnScreen();
  nmsGuiContainer.fadeContainer();
  dialogStage.showAndWait();
  nmsGuiContainer.unfadeContainer();
  return res[0];
}

Here is a screenshot of the alertbox:

This is the screenshot of the alertbox

Einzig7
  • 543
  • 1
  • 6
  • 22
  • 2
    Trying to read this code makes my head hurt. But you don't need CSS to center an `Alert`, you just need to call the `initOwner()` method of the alert. You do call that here, and pass `owner` to it, but the code you posted doesn't indicate what `owner` is. – Zephyr Jul 16 '18 at 05:09

1 Answers1

1

The Stage.initOwner() method does exactly what you need. While you do call it in your example code, I do not know what owner you are passing to it.

Here is a sample that demonstrates how to do this.

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        VBox root = new VBox(10);
        root.setAlignment(Pos.CENTER);
        root.setPadding(new Insets(10));

        Button btnShowAlert = new Button("Show Alert!");

        // Set the action to show the alert
        btnShowAlert.setOnAction(e -> {
            Alert alert = new Alert(Alert.AlertType.WARNING);
            alert.setHeaderText("This is centered over the main window!");
            alert.setContentText("Move the main window and show the alert again!");

            alert.initOwner(primaryStage);
            alert.showAndWait();

        });

        root.getChildren().add(btnShowAlert);

        primaryStage.setTitle("Centered Alerts");
        primaryStage.setScene(new Scene(root));
        primaryStage.setWidth(500);
        primaryStage.setHeight(300);

        primaryStage.show();
    }
}
Zephyr
  • 9,885
  • 4
  • 28
  • 63