How to create dialog in JavaFX


Question

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?

1
0
4/18/2013 10:55:10 AM

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

2
4/30/2013 7:34:43 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon