Search code examples
javamouseeventactionlistenermouseclick-event

Is there a way to set an action command for something that a mouse adapter has been added to?


If I have 5 JPanels with the same mouse adapter and I would like to determine which panel was clicked, how would I do that?


Solution

  • MouseEvent is a subclass of ComponentEvent, so:

    http://download.oracle.com/javase/6/docs/api/java/awt/event/ComponentEvent.html#getComponent%28%29

    getComponent

    public Component getComponent()
    

    Returns the originator of the event.

    Returns: the Component object that originated the event, or null if the object is not a Component.