提问者:小点点

如何在当前初级阶段设置警告框位置?(JavaFX)


编辑:如果用户单击“删除”以删除列表视图中的项目,我会弹出一个警报框。它有效,但我希望它能在原始舞台上弹出。它出现在我的第一台显示器上。有没有办法设置警报显示时的位置?

注意,“owner”在不同的类中,我用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);
}
}

下面是控制器类中的工作代码。没有必要实现这个警报的模态,但是这将是一个很好的补充,使它更加方便。我只是不知道如何将主窗口从主类传递到这个:

    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个答案

匿名用户

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

}