Peter Penzov
Peter Penzov

Reputation: 1754

How to create dialog in JavaFX

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?

Upvotes: 0

Views: 5320

Answers (3)

Dean Chiu
Dean Chiu

Reputation: 1365

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());
    }

Upvotes: 1

AsirC
AsirC

Reputation: 459

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

Upvotes: 2

worcin
worcin

Reputation: 129

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

Upvotes: 2

Related Questions