tags:

views:

45

answers:

2

I have a main window:

public class MainPanel extends JFrame implements MouseListener {

   public MainPanel() {
      setLayout(new FlowLayout());
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      addMouseListener(this);

      ChildPanel child = new ChildPanel();
      add(child);

      JPanel spacer = new JPanel();
      spacer.setPreferredSize(new Dimension(50, 50));
      add(spacer);

      pack();
      setLocationRelativeTo(null);
   }

   @Override
   public void mouseClicked(MouseEvent e) {
      System.out.println("Mouse click event on MainPanel");
   }
}

And a child JPanel:

public class ChildPanel extends JPanel implements MouseListener {

   public ChildPanel() {
      setBackground(Color.RED);
      setPreferredSize(new Dimension(200, 200));
      //addMouseListener(this);
   }

   @Override
   public void mouseClicked(MouseEvent e) {
      System.out.println("Mouse click event on ChildPanel");
   }
}

With the call to addMouseListener commented out in the child panel, the parent receives click events when I click anywhere in the window, including on the child. If I uncomment that call and click on the child panel, only the child receives the click event and it doesn't propagate to the parent.

How do I stop the event from being consumed by the child?

+2  A: 

I don't think you can. I believe it's a Swing design principle that only one component receives an event.

You can get the behavior you want, however, but pass the JFrame to the ChildPanel and calling its mouseClicked(MouseEvent) or whatever method you want. Or just get the parent component.

   @Override
   public void mouseClicked(MouseEvent e) {
      System.out.println("Mouse click event on ChildPanel");
      this.frame.mouseClicked(e);
      getParent().mouseClicked(e);
   }
The Alchemist
I'm wary of the tight coupling implied by this approach.
trashgod
Looks like I'm going to go with getParent() unless I can think of a way to change my project so both the parent and child don't need to handle the event. Restricting event handling to one component seems like a ridiculous design decision to me...
takteek
+2  A: 

In Swing, you generally want the clicked component to respond; but you can forward the mouse event to the parent, as shown below. Here's a related example.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;

/** @see http://stackoverflow.com/questions/3605086 */
public class ParentPanel extends JPanel {

    public ParentPanel() {
        this.setPreferredSize(new Dimension(640, 480));
        this.setBackground(Color.cyan);
        this.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("Mouse clicked in parent panel.");
            }
        });
        JPanel child = new JPanel();
        child.setPreferredSize(new Dimension(320, 240));
        child.setBackground(Color.blue);
        child.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("Mouse clicked in child panel.");
                ParentPanel.this.processMouseEvent(e);
            }
        });
        this.add(child);
    }

    private void display() {
        JFrame f = new JFrame("MouseEventTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ParentPanel().display();
            }
        });
    }
}
trashgod