Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JFXDialog using JFoenix

Tags:

java

javafx

I am using Jfoenix, JFXDialog does not appears.

What am I doing wrong ? Here is the code :

JFXDialogLayout content= new JFXDialogLayout();
content.setHeading(new Text("Error, No selection"));
content.setBody(new Text("No student selected"));
StackPane stackpane = new StackPane();
JFXDialog dialog =new JFXDialog(stackpane, content, JFXDialog.DialogTransition.CENTER);
JFXButton button=new JFXButton("Okay");
button.setOnAction(new EventHandler<ActionEvent>(){
    @Override
    public void handle(ActionEvent event){
        dialog.close();
    }

});
content.setActions(button);
dialog.show();
like image 724
ARKhan Avatar asked Mar 20 '17 13:03

ARKhan


1 Answers

What you are doing is, you are adding your JFXDialog to a StackPane and showing your dialog. By

JFXDialog dialog =new JFXDialog(stackpane, content, JFXDialog.DialogTransition.CENTER);
....
....
....
dialog.show();

Actually what this does is showing the dialog inside the stackpane your dialog got created. The problem is the stackpane is never shown, so the dialog never gets shown too. It is like trying to open document while your computer is turned off.

First of all, this part is completely correct

JFXDialogLayout content= new JFXDialogLayout();
content.setHeading(new Text("Error, No selection"));
content.setBody(new Text("No student selected"));
StackPane stackpane = new StackPane();
JFXDialog dialog =new JFXDialog(stackpane, content, JFXDialog.DialogTransition.CENTER);
JFXButton button=new JFXButton("Okay");
button.setOnAction(new EventHandler<ActionEvent>(){
    @Override
    public void handle(ActionEvent event){
        dialog.close();
    }
});
content.setActions(button);

After here if stackpane is your root Pane just create scene from that and show primaryStage and dialog respectively(actually you do not have to show primaryStage first and dialog second but this order is more meaningful).

Scene scene = new Scene(stackpane, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
dialog.show();

If you already have another root Pane add the stackpane to it before showing the primaryStage and the dialog.

AnchorPane root = new AnchorPane();
....
.... //Some other Nodes here
JFXDialogLayout content= new JFXDialogLayout();
content.setHeading(new Text("Error, No selection"));
content.setBody(new Text("No student selected"));
StackPane stackPane = new StackPane();
JFXDialog dialog =new JFXDialog(stackPane, content, JFXDialog.DialogTransition.CENTER);
JFXButton button=new JFXButton("Okay");
button.setOnAction(new EventHandler<ActionEvent>(){
    @Override
    public void handle(ActionEvent event){
        dialog.close();
    }
});
content.setActions(button);
Scene scene = new Scene(root, 300, 250);
root.getChildren().add(stackPane);
primaryStage.setScene(scene);
primaryStage.show();
dialog.show();

primaryStage comes from the overrided start method of the Application class. Below is the one with the root pane as the stackpane.

@Override
public void start(Stage primaryStage) throws Exception {

    JFXDialogLayout content= new JFXDialogLayout();
    content.setHeading(new Text("Error, No selection"));
    content.setBody(new Text("No student selected"));
    StackPane stackpane = new StackPane();
    JFXDialog dialog =new JFXDialog(stackpane, content, JFXDialog.DialogTransition.CENTER);
    JFXButton button=new JFXButton("Okay");
    button.setOnAction(new EventHandler<ActionEvent>(){
        @Override
        public void handle(ActionEvent event){
            dialog.close();
        }
    });
    content.setActions(button);

    Scene scene = new Scene(stackpane, 300, 250);
    primaryStage.setScene(scene);
    dialog.show();
    primaryStage.show();
}
like image 72
Eralp Sahin Avatar answered Nov 16 '22 18:11

Eralp Sahin