Java MouseListener?

Use MouseAdapter() An abstract adapter class for receiving mouse events. The methods in this class are empty. This class exists as convenience for creating listener objects.So you need to implement only the method you like such as following example: import java.awt.event.

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

JPanel; public class MainClass extends JPanel { public MainClass() { addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent me) { System.out. Println(me); } }); } public static void main(String args) { JFrame frame = new JFrame(); frame.getContentPane(). Add(new MainClass()); frame.

SetDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); frame. SetSize(200, 200); frame.

SetVisible(true); } }.

Thanks. I feel sorry to be asking this but im just beginning with java. I was wondering how to implement MouseAdapter separately so that the code is not inline?

I cannot extend as im already extending JFrame. – iGuygar Apr 19 '10 at 15:56 1 You could create a separate class say, MyMouseListeneras: public class MyMouseListener extends MouseAdapter(){ public void mosuePressed(MouseEvent me){ //whatever you want to happen } } and then add that as mouse listener in your UI code as: addMouseListener(new MyMouseListener()); Now one way to make your UI elements available to your newly implemented MyMouseListener is via constructor parameters. If you want details/code, let me know.

– ring bearer Apr 19 '10 at 16:16 thats great. I have done exactly as you said and have this functionality now. Thank you.

– iGuygar Apr 19 '10 at 16:20.

You can extend MouseAdapter instead, and just override the events you're really interested in.

One could use a MouseAdapter class, which implements the MouseListener interface, so one does not need to implement all the methods. However, by overriding the methods of interest, one can get the desired behavior. For example, if one overrides the mouseClicked method, then one can define some behavior for the mouse click event.

For example (untested code): JLabel label = new JLabel(" AddMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { System.out. Println("Clicked! "); } }); In the code above, the JLabel will print "Clicked!" to the console upon being clicked on.

2 +1 Mouse Adapter does exist exactly for this. – OscarRyz Apr 19 '10 at 15:44.

You can inherit from java.awt.event. MouseAdapter and only override the methods for the event(s) you are interested in.

This search engine searches the API documentation of the Java Developer Kits (JDK). New sources are added as soon as new JDK releases are available.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions