如何在同一JavaFX控制器中实现删除确认弹窗?
Got it, let's fix this problem step by step. The core issue here is that your separate popup controller couldn't access the delete logic in your original controller—so merging them into one class makes total sense. Here's how to adapt your existing FXML and code:
Step 1: Update Your FXML Controller Reference
First, modify your popup FXML to point to your original controller class instead of the separate DeletePopupController. Replace the fx:controller value with the full class name of your main controller (where your delete logic lives):
<AnchorPane id="AnchorPane" prefHeight="89.0" prefWidth="388.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="finalprojectjava.YourMainController"> <children> <Label layoutX="48.0" layoutY="14.0" prefHeight="32.0" prefWidth="293.0" text="Are you sure you want to delete contact?"> <font> <Font size="16.0" /> </font> </Label> <Button layoutX="92.0" layoutY="50.0" mnemonicParsing="false" onAction="#acceptButton" prefHeight="25.0" prefWidth="91.0" text="Yes" /> <Button layoutX="195.0" layoutY="50.0" mnemonicParsing="false" onAction="#declineButton" prefHeight="25.0" prefWidth="91.0" text="No" /> </children> </AnchorPane>
(Replace finalprojectjava.YourMainController with your actual controller's full class path.)
Step 2: Add Popup Logic to Your Main Controller
In your original controller class, add code to load the popup FXML, display it as a modal window, and handle the Yes/No button actions directly. This way, you can call your existing delete logic without any cross-controller issues.
import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Modality; import javafx.stage.Stage; import java.io.IOException; public class YourMainController { // Replace this with your actual delete logic (e.g., removing from a list, updating a database) private void performDelete() { // Your existing delete code goes here System.out.println("Contact deleted successfully!"); } // Method to open the delete confirmation popup (bind this to your main delete button's onAction) @FXML private void openDeletePopup() { try { // Load the popup FXML (make sure the path matches where your FXML file is stored) FXMLLoader loader = new FXMLLoader(getClass().getResource("DeletePopup.fxml")); Parent popupRoot = loader.load(); // Create a modal stage for the popup (blocks interaction with the main window until closed) Stage popupStage = new Stage(); popupStage.setTitle("Confirm Delete"); popupStage.setScene(new Scene(popupRoot)); popupStage.initModality(Modality.APPLICATION_MODAL); popupStage.showAndWait(); // Wait until the popup is closed before resuming } catch (IOException e) { e.printStackTrace(); } } // Handler for the "Yes" button in the popup @FXML private void acceptButton() { // Execute your delete logic directly performDelete(); // Close the popup closePopup(); } // Handler for the "No" button in the popup @FXML private void declineButton() { // Just close the popup without deleting closePopup(); } // Helper method to close the popup stage private void closePopup() { // Get the button that triggered the action, then traverse up to get the stage Button sourceButton = (Button) javafx.scene.control.ContextMenu.getOwnerNode(); Stage popupStage = (Stage) sourceButton.getScene().getWindow(); popupStage.close(); } }
Key Notes:
- Modal Window: Using
Modality.APPLICATION_MODALensures the user can't interact with the main window until they confirm or cancel the delete, which is standard for confirmation dialogs. - Direct Logic Access: Since the popup's buttons are now bound to methods in your main controller, you can call your existing delete code directly without needing to pass references between controllers.
- FXML Path: Double-check the path in
getResource("DeletePopup.fxml")matches where your FXML file is located relative to your controller class.
内容的提问来源于stack exchange,提问作者ivan l

