PInputEvent.getSourceSwingEvent() returns the underlying swing event - InputEvent, from that getSource()
should give you the source, ie:
event.getSourceSwingEvent().getSource()
EDIT:
It appears that event.getSourceSwingEvent().getSource()
is always a canvas. It makes sense as the event actually originated on canvas.
event.getPickedNode()
is the actual picked node. It may be a child of a node that have a registered listener, such as circle3
in the posted sample code.
I am not sure what is the scenario and the reason for finding a node that registered a listener. In most cases, the desired output is the picked node. To find what you're looking for you may have a custom designated extension of PInputEventListener
that can hold a reference to a node that registers it. It may be an overkill, depending on a situation. For example:
public static class MyEventHandler extends PBasicInputEventHandler {
private PNode node;
public MyEventHandler(PNode node) {
this.node = node;
}
public PNode getNode() {
return this.node;
}
}
Another hacky way that comes to mind is to traverse either a stack of objects in a pick path or an hierarchy of nodes to find a node that has this listener. For example to traverse the hierarchy of nodes (similarly it is also possible to enumerate nodes in event.getPath().getNodeStackReference()
) :
public static PNode getRealEventSource(PInputEvent event, PInputEventListener listener) {
PNode node = event.getPickedNode();
while(node != null){
EventListenerList listeners = node.getListenerList();
if (listeners != null) {
if (Arrays.asList(listeners.getListenerList()).contains(listener)) {
return node;
}
}
node = node.getParent();
}
return null;
}
And then, invoke it like this from within the mouseClicked()
method:
System.out.println("The real source:" + getRealEventSource(event, this));