1 Reply Latest reply: Feb 28, 2013 9:27 AM by James_D RSS

    MouseListener not responding.

    993750
      I'm working on a school project to make a video player/image gallery/Paint rip-off. I'm having a problem with a MouseListener that's not responding and I don't know how to get around it.

      (Note: I know that combining Swing and JavaFX is error-prone and not ideal, however this class focuses on Swing and so we must use it)
      public class DisplayWindow implements MouseListener {
      
           JFrame frame;
           private MouseListener[] listeners;
      
           public static void main(String[] args) {
                EventQueue.invokeLater(new Runnable() {
                     public void run() {
                          try {
                               DisplayWindow window = new DisplayWindow();
                               window.frame.setVisible(true);
                          } catch (Exception e) {
                               e.printStackTrace();
                          }
                     }
                });
           }
      
           public DisplayWindow() {
                initialize();
           }
      
           private void initialize() {
                frame = new Welcome(this);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           }
      
           @Override
           public void mouseClicked(MouseEvent e) {
                JPanel p = (JPanel) e.getSource();
      
                switch (p.getName()) {
      
                case "videoPanel" :
                     System.out.println("run videobrowser");
                     break;
      
                }
           }
       public class Welcome extends JFrame {
      
           public JPanel imagePanel;
           public JPanel videoPanel;
           public JPanel doodlePanel;
           DisplayWindow listener;
      
           public Welcome(DisplayWindow listener) {
                this.listener = listener;
                getContentPane().setBackground(Color.DARK_GRAY);
                setBounds(100, 100, 800, 600);
                
                videoPanel = new JPanel();
                videoPanel.setName("videoPanel");
                videoPanel.addMouseListener(listener);
                FlowLayout videoLayout = (FlowLayout) videoPanel.getLayout();
                videoLayout.setAlignment(FlowLayout.LEFT);
                videoLayout.setHgap(-5);
                videoLayout.setVgap(-5);
      
                Dimension d = new Dimension(243, 163);
                File videoFile = new File("footballfail.mp4");
      
                try {
                     imagePanel.add(new WelcomeImageLoader(imageFile, d));
                     doodlePanel.add(new WelcomeImageLoader(doodleFile, d));
                     videoPanel.add(new WelcomeVideoLoader(videoFile, d));
                } catch (IOException e) {
                     e.printStackTrace();
                }
      
                getContentPane().setLayout(groupLayout);
           }
      }
      public class WelcomeVideoLoader extends JPanel {
           
           public WelcomeVideoLoader(File videoFile, final Dimension d) {
      
                Media media = new Media(videoFile.toURI().toString());
      
                final JFXPanel jfxPanel = new JFXPanel();
                //final Dimension dim = d;
                final Group root = new Group();
      
                MediaPlayer player = new MediaPlayer(media);
                player.play();
                player.setCycleCount(MediaPlayer.INDEFINITE);
                player.setMute(true);
                MediaView view = new MediaView(player);
                
                DoubleProperty width = view.fitWidthProperty();
                DoubleProperty height = view.fitHeightProperty();
      
                width.bind(Bindings.selectDouble(view.sceneProperty(), "width"));
                height.bind(Bindings.selectDouble(view.sceneProperty(), "height"));
                
                view.setTranslateX(13);
                view.setPreserveRatio(true);
                root.getChildren().add(view);
                
                Platform.runLater(
                          new Runnable() {
                               @Override
                               public void run() {
                                    Scene scene = new Scene(root, d.getWidth(), d.getHeight(), Color.valueOf("#404040"));
                                    jfxPanel.setScene(scene);
                               }
                          });
      
                super.add(jfxPanel);
      
           }
      }
      The video is looping fine in videoPanel but as I said before, MouseListener is no longer responding. I have more JPanels (images, not videos, so I don't have a problem with threads interfering) in class Welcome but I didn't include them to keep the code short. Their MouseListeners are responding though.

      Does anyone know why the MouseListener on videoPanel is not responding and what I can do to fix this? I'm still new to programming, so if you know the answer, please explain it like you're explaining it to a chimp.
        • 1. Re: MouseListener not responding.
          James_D
          I mocked up something similar to try to see what is going on. It appears that adding a mouse listener directly to a JFXPanel will work, but the mouse event will not get propagated to a JPanel that contains the JFXPanel. In your case, your JFXPanel is contained in a JPanel (the WelcomeVideoLoader), which is contained in the videoPanel, to which the mouse listener is attached. I think you somehow need to arrange for a mouse listener to be registered with the JFXPanel.