Jing
Jing

Reputation: 83

JavaFX Alert doesn't open in new window but in a new tab

I am using JavaFX 8 with scene builder and I am trying to create an alert popup window. See my code below:

Updated: Including all the code I have (excluding some personal identifiers).

public class MainViewController {

    @FXML
    private void handleQuitButtonAction(ActionEvent event) {
        System.exit(0);
    }

    @FXML
    private void handleImportButtonAction(ActionEvent event) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Select the file to import");
        File file = fileChooser.showOpenDialog(new Stage());
        Alert alert;
        if (FileAccess.importFile(file)) {
            alert = new Alert(AlertType.INFORMATION);
            alert.setTitle("Success!");
            alert.setHeaderText("Congrats!");
            alert.setContentText("Successfully imported data from the chosen file!");
        } else {
            alert = new Alert(AlertType.ERROR);
            alert.setTitle("Error!");
            alert.setHeaderText("Sorry...");
            alert.setContentText("Something wrong with the import, please try again.");
        }
        alert.showAndWait();

        // Refresh Scroll List
    }

    @FXML
    private void handleExportButtonAction(ActionEvent event) {
    }
}

public class MainApp extends Application {

    private Stage primaryStage;
    private BorderPane mainView;

    @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("Main System");
        showMainView();
    }

    private void showMainView() {
        try {
            // Load Main View from FXML file.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("view/MainView.fxml"));
            mainView = (BorderPane) loader.load();

            // Show the scene containing the root layout.
            Scene scene = new Scene(mainView);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Stage getPrimaryStage() {
        return primaryStage;
    }

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

public class FileAccess {

    public static boolean importFile(File file) {
        return false;
    }
}

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
  <?import javafx.scene.control.Button?>
    <?import javafx.scene.control.ButtonBar?>
      <?import javafx.scene.control.ScrollPane?>
        <?import javafx.scene.effect.DropShadow?>
          <?import javafx.scene.layout.BorderPane?>

            <BorderPane prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.MainViewController">
              <center>
                <ScrollPane prefWidth="800.0" BorderPane.alignment="CENTER" />
              </center>
              <top>
                <ButtonBar prefHeight="40.0" prefWidth="200.0" BorderPane.alignment="CENTER">
                  <buttons>
                    <Button focusTraversable="false" mnemonicParsing="false" onAction="#handleImportButtonAction" text="Import" ButtonBar.buttonData="LEFT" />
                    <Button focusTraversable="false" mnemonicParsing="false" onAction="#handleExportButtonAction" text="Export" ButtonBar.buttonData="LEFT" />
                    <Button focusTraversable="false" mnemonicParsing="false" onAction="#handleQuitButtonAction" text="Quit Program" ButtonBar.buttonData="RIGHT" />
                  </buttons>
                  <effect>
                    <DropShadow />
                  </effect>
                  <BorderPane.margin>
                    <Insets left="10.0" right="10.0" />
                  </BorderPane.margin>
                </ButtonBar>
              </top>
            </BorderPane>

However, instead of opening up a new window, it opens the alert in a new tab (I don't have anything related to tab set up), see below screenshot:

enter image description here

It doesn't even size correctly...

I am using macOS Mojave.

Thank you!

Upvotes: 2

Views: 1456

Answers (3)

suseboy
suseboy

Reputation: 1

@Jing solution works fine, but if you do not want to enforce your users to modify their OS settings, you need to assign a parent to the JavaFX Alert or any Dialog component. Alert extends Dialog.

private void setDefaultOwnerIfNotExisting(Dialog<?> dialog, Window parent)         {
    if (dialog.getOwner() == null) {
        dialog.initOwner(parent);
    }
}

This has been tested on MacOS Monterey and now my alerts do not come as new tabs.

Upvotes: 0

Jing
Jing

Reputation: 83

Solved:

It is a OS setting issue. In Dock of System Preferences, I had set Prefer tabs when opening documents to Always, after setting it to Manually, it's working properly.

Thanks everyone for the help.

Upvotes: 5

Azhar
Azhar

Reputation: 74

I think you should not create a new stage object. That might be the issue.

Stage stage = (Stage)((Node)(event).getSource()).getScene().getWindow();

The above code would give you the current stage. this might fix your problem.

Good Luck

Upvotes: 0

Related Questions