简体   繁体   中英

In Java, one of my alert boxes is showing twice and the nested alert box is not showing at all

I am working on a school project where I am working with a mySQL database. The specific part I'm working on right now is what happens when a user of the system attempts to delete an appointment from the database. I have three alerts, one works, one is presenting twice, and the other isn't working at all. The first loop, the one that verifies a choice is selected does work. The second one that confirms if the user wants to delete runs twice, ie when you click the OK button it shows again and you must click again. Then, it seems to skip to the bottom where I reload the page. When it reloads the page I can see that the appointment was successfully deleted and it also shows as deleted in the mysql work bench. So it's only the middle alert that doesn't seem to run at all. I have scoured the internet to find out why one is showing twice and the other not at all and although I have found similar questions and problems I tried using their solutions and I did not see any difference. I appreciate any help in the right direction be it code correction or resources. Thank you very much in advance.

// delete selected appointment
    @FXML
    void handleDelete(MouseEvent event) throws IOException, SQLException {
        Appointment ifSelected = appointmentTable.getSelectionModel().getSelectedItem();


    if (ifSelected == null){
        Alert alert = new Alert(Alert.AlertType.WARNING);
        alert.setTitle("Deletion Error");
        alert.setHeaderText("You didn't choose an appointment to delete.");
        alert.setContentText("Please click ok in order to choose an appointment for deletion.");
        alert.showAndWait();
    }

    else{
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setTitle("Appointment Deletion");
        alert.setHeaderText("You are about to delete an appointment record permanantly.");
        alert.setContentText("If you want to proceed, click ok.");

        Optional<ButtonType> result = alert.showAndWait();
        if (result.isPresent() && result.get() == ButtonType.OK){
            Alert alert2 = new Alert(Alert.AlertType.INFORMATION);
            alert2.setTitle("Deletion Successful");
            alert2.setHeaderText("You successfully deleted the appointment with " + ifSelected.getCustomerName()+ " at " + ifSelected.getStartTime() + ".");
            alert.setContentText("If you want to proceed, click ok.");
            alert.show();


            Statement stmt = conn.createStatement();
            apptID = ifSelected.getAppointmentID();
            String sqlDeleteAppointment = "DELETE FROM appointment WHERE appointmentId = " + apptID;
            Query.makeQuery(sqlDeleteAppointment);


            Parent root = FXMLLoader.load(getClass().getResource("appointmentScreen.fxml"));
            scene = new Scene(root);
            stage.setScene(scene);
            stage.show();

    }
}}

'''

You have created alert2 object from Alert class for your nested Alerts. But you have used alert object instead of alert2 .

Alert alert2 = new Alert(Alert.AlertType.INFORMATION);
alert2.setTitle("Deletion Successful");
alert2.setHeaderText("You successfully deleted the appointment with " + ifSelected.getCustomerName()+ " at " + ifSelected.getStartTime() + ".");
alert2.setContentText("If you want to proceed, click ok.");
alert2.show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM