5

In my application, I want to have a Swing panel with JavaFX control over it. For that, I use a JLayeredPane in which I insert a JPanel and a JFXPanel with a Scene that is not filled (aka setFill(null)). But no event passes through transparent areas of the JFXPanel to the Swing panel.

Is there any solution to this problem ?

Thanks

Here an example :

    public class TestJavaFX
    {
        private static JButton button;
        private static JFXPanel javafxPanel;

        public static void main(String[] args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    initAndShowGUI();
                }
            });
        }

        public static void initAndShowGUI()
        {
            JFrame frame = new JFrame("Swing application");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            javafxPanel = new JFXPanel();
            button = new JButton("Swing - Push me");

            JLayeredPane pane = new JLayeredPane();
            pane.add(button, JLayeredPane.DEFAULT_LAYER);
            pane.add(javafxPanel, JLayeredPane.PALETTE_LAYER);
            pane.addComponentListener(new ComponentListener()
            {
                @Override
                public void componentShown(ComponentEvent e)
                {
                }

                @Override
                public void componentResized(ComponentEvent e)
                {
                    button.setBounds(20, 50, 150, 30);
                    javafxPanel.setBounds(0, 0, e.getComponent().getWidth(), e.getComponent().getHeight());
                }

                @Override
                public void componentMoved(ComponentEvent e)
                {
                }

                @Override
                public void componentHidden(ComponentEvent e)
                {
                }
            });

            frame.getContentPane().add(pane, BorderLayout.CENTER);

            Platform.runLater(new Runnable()
            {
                public void run()
                {
                    createScene();
                }
            });

            // Show frame.
            frame.setSize(600, 400);
            frame.setVisible(true);
        }

        private static void createScene()
        {
             Button btn = new Button();
             btn.setText("JavaFX - Push me");

             VBox pane = new VBox();
             pane.getChildren().add(btn);
             Scene scene = new Scene(pane);
             scene.setFill(null);
             javafxPanel.setScene(scene);
        }
   }
PascalW
  • 61
  • 5
  • I think your problem is that you need to to set your Componenet listener to javafxPanel.addComponentListener instead of pane.addComponentListener. Try that and tell me what happens. – DrinkJavaCodeJava Dec 13 '12 at 16:11
  • Thanks, but my problem is not about resizing problem of the components but it is about not receiving events like mouse events. I found a solution by adding an event listener on the JavaFX scene and sending them to the underlying Swing component. But, I'd like to have a cleaner solution. – PascalW Dec 14 '12 at 08:37
  • I'm afraid converting the events manually is actually the way to go here. I'm not aware of a more general event forwarding mechanism. – sarcan Dec 14 '12 at 13:38
  • I arrived at this conclusion by searching on internet but I wanted a confirmation ! Thanks, anyway. – PascalW Dec 17 '12 at 10:15
  • 2
    @pascal please post your answer and set it as a solution – drzymala Feb 11 '13 at 01:14
  • So there is no solution to this problem. – PascalW Nov 10 '17 at 09:55

0 Answers0