简体   繁体   English

Swing 和 Javafx 之间的互操作性:来自嵌入式 Z198B44A40AA77F2256886010C7526 应用程序的关闭父级 Javafx 阶段

[英]Interoperability between Swing and Javafx : Closing parent Javafx stage from embedded Swing application

I have embedded a swing application in a JavaFx application using SwingNode as shown below:我使用 SwingNode 在 JavaFx 应用程序中嵌入了 swing 应用程序,如下所示:

final SwingNode swingNode = new SwingNode();
SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
            swingNode.setContent(new ButtonHtml());        
            }          
        });

The ButtonHtml is a swing application as shown below: ButtonHtml 是一个 swing 应用程序,如下所示:

public class ButtonHtml extends JPanel
                            implements ActionListener {
    protected JButton b1, b3;

    public ButtonHtml() {
        ImageIcon buttonIcon = createImageIcon("images/down.gif");      

        b1 = new JButton("<html><center><b><u>D</u>isable</b><br>"
                         + "<font color=#ffffdd>FX button</font>", 
                         buttonIcon);
        Font font = b1.getFont().deriveFont(Font.PLAIN);
        b1.setFont(font);
        b1.setVerticalTextPosition(AbstractButton.CENTER);
        b1.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales
        b1.setMnemonic(KeyEvent.VK_D);
        b1.setActionCommand("disable");


        b3 = new JButton("<html><center><b><u>E</u>nable</b><br>"
                         + "<font color=#ffffdd>FX button</font>", 
                         buttonIcon);
        b3.setFont(font);
        //Use the default text position of CENTER, TRAILING (RIGHT).
        b3.setMnemonic(KeyEvent.VK_E);
        b3.setActionCommand("enable");
        b3.setEnabled(false);

        //Listen for actions on buttons 1 and 3.
        b1.addActionListener(this);
        b3.addActionListener(this);

        b1.setToolTipText("Click this button to disable the FX button.");
        b3.setToolTipText("Click this button to enable the FX button.");

        //Add Components to this container, using the default FlowLayout.
        add(b1);
        add(b3);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if ("disable".equals(e.getActionCommand())) {
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    //close the parent javafx window

                }
            });


            b1.setEnabled(false);
            b3.setEnabled(true);
        } else {
            Platform.runLater(new Runnable() {
               @Override
               public void run() {
                   //do something
               }
            });
            b1.setEnabled(true);
            b3.setEnabled(false);
        }
    }

    /** Returns an ImageIcon, or null if the path was invalid.
     * @param path
     * @return  */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = ButtonHtml.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

}

Now in my javafx application, I have embedded the SwingNode in a pane and created a scene and doing stage.show to open the javafx window:现在在我的 javafx 应用程序中,我将 SwingNode 嵌入到窗格中并创建了一个场景并执行 stage.show 以打开 javafx window:

BorderPane pane = new BorderPane();

        Image fxButtonIcon = new Image(getClass().getResourceAsStream("images/middle.gif"));

        fxbutton = new Button("FX button", new ImageView(fxButtonIcon));
        fxbutton.setTooltip(new Tooltip("This middle button does nothing when you click it."));
        fxbutton.setStyle("-fx-font: 22 arial; -fx-base: #cce6ff;");
        pane.setTop(swingNode);
        pane.setCenter(fxbutton);


        Scene scene = new Scene(pane, 300, 100);
        stage.setScene(scene);
        stage.setTitle("Enable FX Button");
        stage.show();

I need to know, how can we trigger the hide/dispose/close of the parent Javafx window from the embedded Swing application.我需要知道,我们如何从嵌入式 Swing 应用程序触发父 Javafx window 的隐藏/处置/关闭。 Basically I want on click on a button (in above example say button b1), and close the javafx window.基本上我想点击一个按钮(在上面的例子中说按钮 b1),然后关闭 javafx window。 Please note that the swing application and javafx application are independent.请注意,swing 应用程序和 javafx 应用程序是独立的。

A fairly clean approach is just to define a field in your ButtonHtml class representing what to do when b1 is pressed.一个相当干净的方法是在ButtonHtml class 中定义一个字段,表示按下b1时要做什么。

Incorporating that, and modernizing the action listener implementations, you can do:结合这一点,并对动作侦听器实现进行现代化改造,您可以执行以下操作:

public class ButtonHtml extends JPanel {

    protected JButton b1, b3;

    private Runnable onCloseRequested = () -> {} ;

    public void setOnCloseRequested(Runnable onCloseRequested) {
        this.onCloseRequested = onCloseRequested ;
    }

    public Runnable getOnCloseRequested() {
        return onCloseRequested ;
    }

    public ButtonHtml() {
        ImageIcon buttonIcon = createImageIcon("images/down.gif");      

        b1 = new JButton("<html><center><b><u>D</u>isable</b><br>"
                         + "<font color=#ffffdd>FX button</font>", 
                         buttonIcon);
        Font font = b1.getFont().deriveFont(Font.PLAIN);
        b1.setFont(font);
        b1.setVerticalTextPosition(AbstractButton.CENTER);
        b1.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales
        b1.setMnemonic(KeyEvent.VK_D);
        b1.setActionCommand("disable");


        b3 = new JButton("<html><center><b><u>E</u>nable</b><br>"
                         + "<font color=#ffffdd>FX button</font>", 
                         buttonIcon);
        b3.setFont(font);
        //Use the default text position of CENTER, TRAILING (RIGHT).
        b3.setMnemonic(KeyEvent.VK_E);
        b3.setActionCommand("enable");
        b3.setEnabled(false);

        //Listen for actions on buttons 1 and 3.
        b1.addActionListener(e -> {
            Platform.runLater(onCloseRequested);
            b1.setEnabled(false);
            b3.setEnabled(true);
        });
        b3.addActionListener(e -> { /* ... */ });

        b1.setToolTipText("Click this button to disable the FX button.");
        b3.setToolTipText("Click this button to enable the FX button.");

        //Add Components to this container, using the default FlowLayout.
        add(b1);
        add(b3);
    }



    /** Returns an ImageIcon, or null if the path was invalid.
     * @param path
     * @return  */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = ButtonHtml.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

}

And then you can just do然后你可以做

final SwingNode swingNode = new SwingNode();
SwingUtilities.invokeLater(() -> {
    ButtonHtml buttonHtml = new ButtonHtml();
    buttonHtml.setOnCloseRequested(() -> swingNode.getScene().getWindow().hide());
    swingNode.setContent(buttonHtml);        
});

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

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