简体   繁体   中英

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:

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:

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:

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. Basically I want on click on a button (in above example say button b1), and close the javafx window. Please note that the swing application and javafx application are independent.

A fairly clean approach is just to define a field in your ButtonHtml class representing what to do when b1 is pressed.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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