0

I tested to create simple dialog in JavaFX but for some reason the code is not working:

MenuBar menuBar = new MenuBar();

        // File menu - new, save, exit
        Menu menu = new Menu("File");
        menu.getItems().add(new MenuItem("New"));
        menu.getItems().add(new MenuItem("Save"));
        menu.getItems().add(new SeparatorMenuItem());

        menuBar.getMenus().add(menu);

        // Options menu - Preferences
        Menu options = new Menu("Options");
        options.getItems().add(new MenuItem("Preferences"));

        menuBar.getMenus().add(options);

        // Help menu - About
        Menu help = new Menu("Help");
        MenuItem about = new MenuItem("Exit");


        about.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                Stage dialogStage = new Stage();
                dialogStage.initModality(Modality.WINDOW_MODAL);
                dialogStage.setScene(new Scene(VBoxBuilder.create().
                        children(new Text("Hi"), new Button("Ok.")).
                        alignment(Pos.CENTER).padding(new Insets(5)).build()));
                dialogStage.show();
            }
        });

        menuBar.getMenus().add(help);

        menuBar.prefWidthProperty().bind(primaryStage.widthProperty());

        root.getChildren().add(menuBar);
        primaryStage.setScene(scene);
        primaryStage.show();

I want when I click on About menu item to display simple dialog window with author information. Can you tell me how to correct my mistake, please?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • 3
    Do you see the About menu item? In the shown code it's not added to a menu... – Puce Apr 18 '13 at 11:15
  • Similar to: [How to create a modal window in JavaFX 2.1](http://stackoverflow.com/questions/10486731/how-to-create-a-modal-window-in-javafx-2-1), [How to create and show common dialog (Error, Warning, Confirmation) in JavaFX 2.0?](http://stackoverflow.com/questions/8309981/how-to-create-and-show-common-dialog-error-warning-confirmation-in-javafx-2) and [JavaFX 2.1 MessageBox](http://stackoverflow.com/questions/11662857/javafx-2-1-messagebox) – jewelsea Apr 19 '13 at 23:37

3 Answers3

2

I use http://sourceforge.jp/projects/jfxmessagebox/wiki/JfxMessageBox for my Messageboxes. I hope this helps.

worcin
  • 129
  • 5
2

You can try my custom Dialog. Visit FXDialog public repository.

AsirC
  • 449
  • 1
  • 6
  • 20
1

I was also looking for FX dialog features for a few days. Before I attempt to write my own complicated dialog, I came across the"savior" ControlsFX [here] (http://fxexperience.com/). It works like a miracle to me. However, You will have to upgrade to JDK8 fully experience the wonder of ControlFX has on FX8. I believe everyone will use JDK8 sooner or later.

It's lucky my original code did not get too far before it becomes too complicated for me to make such transition. :)

Hope this post also feeds 5-thousand JAVAer. Check out how it works with the following sample code:

    Action response = Dialogs.create()
        .owner( null)
        .title("Confirmation")
        .masthead("Are you sure to delete UserLevel: '"+ul.getLevelname()+"' ?")
        .message(entry)
        .showConfirm();

    System.out.println("response: " + response);        


    if (response.toString().equals("YES")){
        if(!Main.db.em.getTransaction().isActive())
            Main.db.em.getTransaction().begin();

        Main.db.em.remove(ul);
        Main.db.em.getTransaction().commit();
        tbvMain.getItems().remove(tbvMain.getSelectionModel().getSelectedIndex());
    }
Dean Chiu
  • 1,325
  • 1
  • 13
  • 13