MouseEvents not fired for both parent and child components?
Hello.
I am writing an application in which there is a JPanel which must do something when it is right-clicked. What is done is based on the coordinates of the click in that panel.
However, the above panel contains many sub-panels. These sub-panels are to handle left-clicks.
In my implementation, it is convenient to have different listeners, one for the main, outer panel, and one for the sub-panels, but whether I use more than one listener seems to have no effect on...
THE PROBLEM
The problem is that only the lowest component in the hierarchy gets an event. By this I mean that only the sub-panels get an event; event.getSource() is the sub-panel clicked. It seems that swing is designed this way.
Can you please recommend a way to get around this. Why are two events not created, one for the parent JPanel, and one for the sub-panel, each handled by its respective listener (which might well be the same, resulting in two calls to mouseClicked)?
Is there any way to send the event back to the parent component if it is a right-click (or for any other reason, of course)?
Or, can I restrict a listeners to handly only clicks of a certain type? Or tell the event generator itself only to fire an event if the click is of a certain type? In the past I have read something about nullifying an event base on the modifiers (i.e., type of click, etc.), but I don't know if that would apply here, or how to use it.
Thanks for any information.
Grape
[1517 byte] By [
ProGrapea] at [2007-11-27 10:25:41]

# 2
> Is there any way to send the event back to the parent component if it is a right-click (or for any other reason, of course)?
Sure, just use the Component.dispatchEvent(...) method to redispatch the event to the parent component.
Check out the example from the "How to Use Glass Panes" tutorial. The code there is more complex then yours would need to be since that code needs to determine which component (under the glass pane) to forward the event to. In your case you know its the parent panel.
http://java.sun.com/docs/books/tutorial/uiswing/components/rootpane.html
# 5
camickr, I have tried using
public void mouseClicked(MouseEvent e) {
if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
((Component)e.getSource()).getParent().dispatchEvent(e);
System.out.println("debug: event from sub-panel");
}
but "debug: event from sub-panel" still prints even though the parent does not share the same listener and does something entirely different.
# 7
nevermind, that was silly of me, of course it's going to execute the rest of mouseClicked =P
Now, the event gets sent to the parent, but the source is still the child. So the coordinates of the event are relative to the child, not to the parent, and I can't do my calculation properly.
What I really need is an event the source of which is the parent component.
Thanks!
# 8
> Now, the event gets sent to the parent, but the source is still the child
> What I really need is an event the source of which is the parent component.
Thats why I gave you the link to the Swing tutoral. It shows you how to:
a) convert the mouse coordinates
b) and how to create a new event with the parent as the source