如何在当前primaryStage上设置Alert box位置? (JavaFX的)

时间:2015-11-21 00:32:47

标签: javafx java-8 alert

编辑: 如果用户单击“删除”以删除ListView中的项目,则会弹出一个警告框。它有效,但我希望它能够超越原始舞台。它出现在我的第一台显示器中。是否有任何方法可以在显示警报时设置警报的位置?

注意,“所有者”在不同的类中,我使用Scenebuilder / FXML创建了所有内容。我无法弄清楚如何让initOwner()工作。这是“主要”类:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Assignment_5 extends Application {
    public Stage primaryStage;

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("Assignment_5.fxml"));
    primaryStage.setTitle("Plant Pack");
    primaryStage.setScene(new Scene(root, 1200, 500));
    primaryStage.show();
}

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

这是Controller类中的工作代码。没有必要实现此警报的模式,但它会是一个很好的补充,使它更方便。我根本不知道如何将主窗口从Main类传递给它:

    protected void handleDeleteButtonClick(ActionEvent event) {
    Alert alertBox = new Alert(Alert.AlertType.CONFIRMATION, "Confirm Delete", ButtonType.OK, ButtonType.CANCEL);
    alertBox.setContentText("Are you sure you want to delete this " + plantType.getValue().toString().toLowerCase() + "?");
    alertBox.showAndWait();
    if(alertBox.getResult() == ButtonType.OK) {
        int selectedPlant = plantList.getSelectionModel().getSelectedIndex();
        observablePlantList.remove(selectedPlant);
    }
    else {
        alertBox.close();
    }
}

我知道这是相当新的,所以很难找到很多资源。如果有人知道我可能错过的任何信息,请告诉我。感谢您提供的任何帮助。 我使用Java 8和IntelliJ 14.1.5。

1 个答案:

答案 0 :(得分:4)

正如@jewelsea建议的那样,设置警报框的模态和所有者将确保警报将出现在舞台上,即使舞台被移动。

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class DeleteAlertDemo extends Application {

    Stage owner;
    ObservableList<String> observablePlantList;
    ListView<String> plantList;

    protected void handleDeleteButtonClick(ActionEvent event) {
        String item = plantList.getSelectionModel().getSelectedItem();
        Alert alertBox = new Alert(Alert.AlertType.CONFIRMATION, "Confirm Delete", 
                ButtonType.OK, ButtonType.CANCEL);
        alertBox.setContentText("Are you sure you want to delete this " 
                + item.toLowerCase() + "?");
        alertBox.initModality(Modality.APPLICATION_MODAL); /* *** */
        alertBox.initOwner(owner);                         /* *** */
        alertBox.showAndWait();
        if (alertBox.getResult() == ButtonType.OK) {
            int selectedPlant = plantList.getSelectionModel().getSelectedIndex();
            observablePlantList.remove(selectedPlant);
        } else {
            alertBox.close();
        }
    }

    @Override
    public void start(Stage primaryStage) {
        owner = primaryStage;                /* *** */
        Button deleteBtn = new Button();
        deleteBtn.setText("Delete");
        deleteBtn.setOnAction(this::handleDeleteButtonClick);

        observablePlantList = FXCollections.observableArrayList("Begonia", 
                "Peony", "Rose", "Lilly", "Chrysanthemum", "Hosta");
        plantList = new ListView<>(observablePlantList);
        plantList.getSelectionModel().select(0);
        BorderPane root = new BorderPane();
        root.setCenter(plantList);
        root.setRight(deleteBtn);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Delete Alert Demo");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}