0

I'm working on an application which is based on swing and javafx 8. In this create a frame in swing and jbutton use and jbutton action code is done in javafx 8 means use scene. in this a alert dialog create.but problem is that if i click anywhere except alert dialog then alert dialog hidden behind swing frame.i want javafx alert dialog keep on swing frame which i create.if i click ok on alert then action is goes to swing frame. please suggest......

here is my code:

    final JFXPanel fxPanel = new JFXPanel();


    Platform.runLater(new Runnable() {
        public void run() {
            initFX(fxPanel);
        }

        private void initFX(JFXPanel fxPanel) {
            // TODO Auto-generated method stub
                Scene scene = createScene();
                fxPanel.setScene(scene);
        }

        private Scene createScene() {

             Group  root  =  new  Group();
             Scene  scene  =  new  Scene(root);

             Alert cn=new Alert(AlertType.CONFIRMATION);
             cn.initStyle(StageStyle.UNDECORATED);

             cn.setTitle(null);
             cn.setHeaderText(null);
             cn.setContentText(ProjectProps.rb.getString("msg.play.confirm"));

             DialogPane dp=cn.getDialogPane();
             dp.getStylesheets().add(getClass().getResource("login.css").toExternalForm());
             dp.setStyle("-fx-border-color: black; -fx-border-width: 5px; ");  

             Optional<ButtonType> result=cn.showAndWait();
             if(result.get()==ButtonType.OK){
                 System.out.println("ok button clicked:"+result.get());
                k=0;
             } else{
                 System.out.println("cancel clicked");
                k=1;
             }
            return (scene);
        }

    }); 
kRiZ
  • 2,320
  • 4
  • 28
  • 39

1 Answers1

1

I'm not sure to understand what you really want to do. Do you absolutely need a JavaFX alert in a swing application? JDialog works very well, and Alert and Dialogs works very well too in a JavaFX context.

Have you tried to set your Alert application modal with cn.initModality(Modality.APPLICATION_MODAL) ?

Maybe you can find you answer in this post : How to make a JFrame Modal in Swing java

This is the suggested code in the post:

final JDialog frame = new JDialog(parentFrame, frameTitle, true);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);

I would create a new JDialog and set as parameter your main frame (parentFrame) and add your JFXPanel to its content.

frame.getContentPane.add(fxPanel)

You can use fxPanel.setScene(yourScene) to add FX content to your JDialog.

Example for the suggestion:

public class Main  {

public static void main(String[] args) {

    // create parent
    JFrame parent = new JFrame("MainFrame");
    parent.setSize(500, 500);

    // center of screen
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    parent.setLocation(dim.width/2-parent.getSize().width/2, dim.height/2-parent.getSize().height/2);
    parent.setVisible(true);

    createDialog(parent);

}

private static void createDialog(JFrame parent) {

    // create dialogs JFX content
    BorderPane layout = new BorderPane();
    layout.setTop(new Button("HELLO"));
    layout.setBottom(new Button("IT'S ME"));
    Scene scene = new Scene(layout);
    JFXPanel dlgContent = new JFXPanel();
    dlgContent.setScene(scene);
    dlgContent.setPreferredSize(new Dimension(200, 200));
    dlgContent.setVisible(true);

    // create dialog and set content
    JDialog dlg = new JDialog(parent, "Dialog", true);
    dlg.setLocationRelativeTo(parent);
    dlg.getContentPane().add(dlgContent);
    dlg.pack();
    dlg.setVisible(true);

}

}

Hope I could help you.

Community
  • 1
  • 1
exe-cute-table
  • 128
  • 1
  • 11