javascriptjavaeventsmethodsmouselistener

Where that instance (e) in this method mouseclicked(MouseListener e) initialized?


in the method mouseClicked (MouseEvent e) can anyone explain to me where that instance (e) initialized ?

@Override

public void mouseClicked(MouseEvent e) {

    if (e.getButton() == MouseEvent.BUTTON1)

        System.out.println("Left button clicked!");

    else if (e.getButton() == MouseEvent.BUTTON3)

        System.out.println("Right button clicked!");

}

Is the System initialized ? And where ?


Solution

  • After you register a MouseListener on a component by calling addMouseListener, that component will call the appropriate method on each MouseListener attached to it whenever a MouseEvent occurs, passing the MouseEvent as the first argument.

    See also the MouseListener documentation:

    The class that is interested in processing a mouse event either implements this interface (and all the methods it contains) or extends the abstract MouseAdapter class (overriding only the methods of interest).
    The listener object created from that class is then registered with a component using the component's addMouseListener method. A mouse event is generated when the mouse is pressed, released clicked (pressed and released). A mouse event is also generated when the mouse cursor enters or leaves a component. When a mouse event occurs, the relevant method in the listener object is invoked, and the MouseEvent is passed to it.