Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a popup window in javafx [duplicate]

I want to create a popup window in a JavaFX application. Give me some ideas.

enter image description here

When I click on Check button it opens the popup window. How to do it?

like image 610
Maulik Patel Avatar asked Mar 04 '14 08:03

Maulik Patel


People also ask

Is JavaFX a Scenebuilder?

Scene Builder is written as a JavaFX application, supported on Windows, Mac OS X and Linux. It is the perfect example of a full-fledge JavaFX desktop application. Scene Builder is packaged as a self contained application, which means it comes bundled with its own private copy of the JRE.


2 Answers

You can either create a new Stage, add your controls into it or if you require the POPUP as Dialog box, then you may consider using DialogsFX or ControlsFX(Requires JavaFX8)

For creating a new Stage, you can use the following snippet

@Override
public void start(final Stage primaryStage) {
    Button btn = new Button();
    btn.setText("Open Dialog");
    btn.setOnAction(
        new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                final Stage dialog = new Stage();
                dialog.initModality(Modality.APPLICATION_MODAL);
                dialog.initOwner(primaryStage);
                VBox dialogVbox = new VBox(20);
                dialogVbox.getChildren().add(new Text("This is a Dialog"));
                Scene dialogScene = new Scene(dialogVbox, 300, 200);
                dialog.setScene(dialogScene);
                dialog.show();
            }
         });
    }

If you don't want it to be modal (block other windows), use:

dialog.initModality(Modality.NONE);
like image 161
ItachiUchiha Avatar answered Oct 05 '22 15:10

ItachiUchiha


The Popup class might be better than the Stage class, depending on what you want. Stage is either modal (you can't click on anything else in your app) or it vanishes if you click elsewhere in your app (because it's a separate window). Popup stays on top but is not modal.

See this Popup Window example.

like image 42
RyanR Avatar answered Oct 05 '22 13:10

RyanR