Search code examples
javaswingawtjavafx-8hwnd

AWT Panel not getting rendered in JFX


I want to add a java.awt.Panel to my JavaFX8 application. Unfortunately it seems that the Panel doesn't get rendered when attached to a SwingNode.

I have a simple testapplication:

import java.awt.Dimension;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.embed.swing.SwingNode;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class AWTInJFX extends Application {

    @Override
    public void start(Stage stage) {

        final AwtInitializerTask awtInitializerTask = new AwtInitializerTask(() -> {
                AWTPanel panel = new AWTPanel();
                return panel;
        });

        SwingNode swingNode = new SwingNode();

        SwingUtilities.invokeLater(awtInitializerTask);
        try {
            swingNode.setContent(awtInitializerTask.get());
        } catch (InterruptedException | ExecutionException ex) {
            Logger.getLogger(AWTInJFX.class.getName()).log(Level.SEVERE, null, ex);
        }

        stage.setScene(new Scene(new Group(swingNode), 600, 600));
        stage.setResizable(false);
        stage.show();       

        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame();
            frame.setSize(new Dimension(600, 400));
            frame.add(new AWTPanel());
            frame.setVisible(true);
        });
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    private class AwtInitializerTask extends FutureTask<JPanel> {
        public AwtInitializerTask(Callable<JPanel> callable) {
            super(callable);
        }
    }

}

My JPanel containing the java.awt.Panel

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Panel;
import javax.swing.JPanel;

public class AWTPanel extends JPanel{
    public AWTPanel()
    {
        Dimension d = new Dimension(600, 400);
        setPreferredSize(d);
        Panel p = new Panel();
        p.setSize(d);
        p.setPreferredSize(d);
        p.setBackground(Color.red);
        this.add(p);
        this.setBackground(Color.green);
    }
}

Other AWT components doesn't show up either when I add my AWTPanel to a SwingNode.

Can somebody explain me why this isn't working?

I need a AWT Panel to get a hWnd used in additonal C++ libraries.


Solution

  • From the SwingNode Javadocs:

    The hierarchy of components contained in the JComponent instance should not contain any heavyweight components, otherwise SwingNode may fail to paint it.

    As far as I know, there is no way to embed a heavyweight component, such as an AWT component, in JavaFX.

    Depending on your requirements, you could consider reversing things around, so that you have a Swing/AWT frame as your main window, and embed the JavaFX part of your application in a JFXPanel.