Issue
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:
It doesn't even size correctly...
I am using macOS Mojave.
Thank you!
Solution
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.
Answered By - Jing
Answer Checked By - Senaida (JavaFixing Volunteer)