简体   繁体   English

摆动框架上的javafx警报对话框

[英]javafx alert dialog on swing frame

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. 我正在开发一个基于swing和javafx 8的应用程序。在此应用程序中,在swing和jbutton中创建一个框架,并在javafx 8中完成jbutton动作代码,即使用场景。 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. 在这个警报对话框中创建。但是问题是,如果我单击警报对话框以外的任何地方,然后将警报对话框隐藏在秋千框架后面。我希望javafx警报对话框继续创建的秋千框架。秋千架。 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);
        }

    }); 

I'm not sure to understand what you really want to do. 我不确定您真正想做什么。 Do you absolutely need a JavaFX alert in a swing application? 在Swing应用程序中,您是否绝对需要JavaFX警报? JDialog works very well, and Alert and Dialogs works very well too in a JavaFX context. JDialog在JavaFX上下文中效果很好,而Alert和Dialogs也很好。

Have you tried to set your Alert application modal with cn.initModality(Modality.APPLICATION_MODAL) ? 您是否尝试使用cn.initModality(Modality.APPLICATION_MODAL)设置Alert应用程序模式?

Maybe you can find you answer in this post : How to make a JFrame Modal in Swing java 也许您可以在这篇文章中找到答案: 如何在Swing Java中制作JFrame模式

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. 我将创建一个新的JDialog并将您的主框架(parentFrame)设置为参数,并将您的JFXPanel添加到其内容中。

frame.getContentPane.add(fxPanel)

You can use fxPanel.setScene(yourScene) to add FX content to your JDialog. 您可以使用fxPanel.setScene(yourScene)将FX内容添加到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. 希望我能为您服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM