【问题标题】:How to set Alert box position over current primaryStage? (JavaFX)如何在当前的primaryStage上设置警报框位置? (JavaFX)
【发布时间】:2015-11-21 00:32:46
【问题描述】:

编辑: 如果用户单击“删除”以删除 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();
    }
}

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

【问题讨论】:

  • jQueryUI 对话框与 JavaFX 对话框有什么相关性?
  • 我没有尝试过,但您可能想在显示警报之前尝试初始化 modality 和 owner。
  • @jewelsea 我不知道。那是 JavaFX 问题中提供的链接。哈哈。我只是说,因为这个网站可能对重复的问题很挑剔。

标签: javafx java-8 alert


【解决方案1】:

正如@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);
    }

}

【讨论】:

  • 一个问题。主舞台在另一个班级,我的“主要”班级。警报是在 Controller 类中创建的。我怎么用那种方法称呼它?我会创建父级的实例吗?
  • 已编辑问题以更详细地显示我的问题。见上文^。
  • 啊。解决方案很简单,但很难看。请参阅stackoverflow.com/questions/11994366/… 的第二个答案和 cmets。您只需在控制器类中包含一个方法,即可设置主要阶段。
  • 或者,如果您不喜欢传递变量,在事件处理程序中您可以执行以下操作:Window owner = ((Node)event.getTarget()).getScene().getWindow(); 这假定 Button(事件目标)与当然是舞台。
猜你喜欢
  • 1970-01-01
  • 2018-10-13
  • 2019-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-18
  • 2020-04-10
相关资源
最近更新 更多