【问题标题】:Interoperability between Swing and Javafx : Closing parent Javafx stage from embedded Swing applicationSwing 和 Javafx 之间的互操作性:从嵌入式 Swing 应用程序关闭父 Javafx 阶段
【发布时间】:2020-05-28 15:06:24
【问题描述】:

我使用 SwingNode 在 JavaFx 应用程序中嵌入了一个 Swing 应用程序,如下所示:

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

ButtonHtml 是一个摇摆应用程序,如下所示:

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;
        }
    }

}

现在在我的 javafx 应用程序中,我将 SwingNode 嵌入到窗格中并创建了一个场景并执行 stage.show 以打开 javafx 窗口:

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();

我需要知道,我们如何从嵌入式 Swing 应用程序触发父 Javafx 窗口的隐藏/处置/关闭。 基本上我想点击一个按钮(在上面的例子中说按钮 b1),然后关闭 javafx 窗口。请注意,swing 应用程序和 javafx 应用程序是独立的。

【问题讨论】:

  • 这里的问题不是很清楚。您似乎只需要从与按钮的动作侦听器关联的Platform.runLater() 代码中调用swingNode.getScene().getWindow().hide()
  • 有点 OT,但如果您的 swing 节点的 only 目的是将 HTML 格式合并到按钮中,则将 WebView 设置为 JavaFX 按钮的图形可能是一种更简单的方法。

标签: java swing javafx


【解决方案1】:

一种相当简洁的方法是在ButtonHtml 类中定义一个字段,表示按下b1 时要做什么。

结合这一点,并对动作侦听器实现进行现代化改造,您可以:

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;
        }
    }

}

然后你就可以这样做了

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

【讨论】:

    猜你喜欢
    • 2013-12-23
    • 2021-10-16
    • 2013-05-29
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 2014-10-01
    • 2014-12-24
    相关资源
    最近更新 更多