0

Im having an issue with making a popup menu work in my code, The way i have it set up, is if a button is clicked, it will open up a new scene(which prompts up if they would like to delete something)

however that new scene is another FXML file with it's own controller, and when I tried to make the new FXML controller delete something, it wouldn't work because the code isn't in the same controller, so i can't execute the code from the FIRST controller.

Now I just want to be able to open up a dialogue within the same class, and I don't know how to convert the code for it to be in the same controller. this is the FXML code that I want to keep in the same controller

<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.DeletePopupController">
   <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>
ivan l
  • 67
  • 1
  • 4
  • 1
    You should use different controllers: you just have to set up the appropriate communication between them. See https://stackoverflow.com/questions/14187963/ – James_D Jun 01 '18 at 18:26
  • It is not clear what you are trying to accomplish, but wouldn't using the JavaFX `Alert` class work for this? https://code.makery.ch/blog/javafx-dialogs-official/ – Zephyr Jun 01 '18 at 23:59

1 Answers1

0

Here is an example of using the JavaFX Dialogs to get response from a user via a popup window. You can learn more about the power of this API here.

public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) {

        VBox pane = new VBox(10);
        pane.setPadding(new Insets(10));

        Button btnShowDialog = new Button("Show Popup");
        // Set the action to call the showPopup() method when clicked
        btnShowDialog.setOnAction(e -> showPopup());

        pane.getChildren().add(btnShowDialog);

        Scene scene = new Scene(pane);
        primaryStage.setScene(scene);

        primaryStage.show();
    }

    private void showPopup() {

        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setTitle("Delete Contact?");
        alert.setHeaderText("Are you sure you want to delete this contact?");

        // Set the available buttons for the alert
        ButtonType btnYes = new ButtonType("Yes");
        ButtonType btnNo = new ButtonType("No");

        alert.getButtonTypes().setAll(btnYes, btnNo);

        // This allows you to get the response back from the user
        Optional<ButtonType> result = alert.showAndWait();

        if (result.isPresent()) {
            if (result.get() == btnYes) {
                System.out.println("User clicked Yes!");
            } else if (result.get() == btnNo) {
                System.out.println("User clicked No!");
            }
        }

    }
}
Zephyr
  • 9,885
  • 4
  • 28
  • 63