How to use MouseListener, MouseAdapter and MouseEvent in Java Swing

preview_player
Показать описание
All Java graphical components that send messages must:
Implement an EventHandler interface, i.e. ActionListener, and overwrite the method actionPerformed().

public interface MouseListener
extends EventListener
The listener interface for receiving "interesting" mouse events (press, release, click, enter, and exit) on a component. (To track mouse moves and mouse drags, use the MouseMotionListener.)

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.

Follow on Facebook:

Subscribe to our other channel:
Telusko Hindi :
Рекомендации по теме
Комментарии
Автор

Thank you very much sir...i helped me a lot.... actually i was frustrated with this concept when I watched this in another platform...but explained this crystal clear... once again thank you very much

akashstark
Автор

Thank You so much :)
Merci :)
شكرا :)

zakariachahboun
Автор

Very concise and informative. Great job!

greyhound
Автор

Good video, basic concept explained nicely and quickly. Helped me add a mouse listener to my canvas.

crazymcfobo
Автор

You're having a hart time avoiding the accent. I think thats adorable ^^

fabbyy
Автор

How do you make that x, y value available out side that method.. What you have shown is an unrealistic example.

fernandohood
Автор

Mine does not work. The form loads but
there is no out put, and the obj object is green underlined and
saying “variable obj is not used”.


package mousetester;

import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;

/** DOES NOT WORK DONT KNOW WHY

public class Mousetester {


public static void main(String[] args) {


Xyz obj = new Xyz();
}

}

class Xyz extends JFrame
{
public Xyz()
{
addMouseListener(new MouseAdapter(){

public void MousePressed(MouseEvent e){


int x = e.getX();
int y = e.getY();

System.out.println(x + ", " + y);

}

});

setLayout( new FlowLayout());
setVisible(true);
setSize(400, 400);


}

}

nigelpallatt
Автор

Hi Navin, Would you be able to make a video on the types of listeners that we use in J2EE components, esp. the business tier. On the web I'm seeing only references to Java GUI/Swing and Servlet related listeners.

madhavilathaannamraju
Автор

Im trying to create my own Button because I want them to have a different look than the default java buttons. addMouseListener is not found. Is it because it does not extend JFrame?

Teamdward
Автор

I don't have an addMouseListener method. How do I get it?

lookupverazhou
Автор

drag by mouse and want to write text which is stored in arraylist. share your video or some another resource.

rudreshmehta
Автор

How do I add a title and GUI? How can I make the:
setLayout(new FlowLayout());
setVisible(true);
setSize(400, 400);

go in
public static void main(String[] args)

someone-cslk
Автор

How do you get the swingcode shortcut to work on intellij? 0:30

ArmanLamei
Автор

I want an mp3 to be played on the click event. What changes do I need to make/add?

rathindam