Adding a mouse listener to a Canvas

Hi all,

I am trying to add a listener to a Canvas so that when I move the mouse it triggers a function.

Could people please check my line of thought to see if I have done anything stupid:

1) First of all I simply used mouseMove(), which worked fine but the documentation says it is now depretiated.  So I decided to do as the documentation recomends and use processMouseMotionEvent()

2) To do this I put the following line into the constructor:
this.addMouseMotionListener(this);

But it seems Canvas does not implement MouseMotionListener, so I created a subclass of Canvas which also implements MouseMotionListener

3)  Then it complains that I also need to add some new functions, even though I don't want them:
  public void mouseDragged(MouseEvent event){}
  public void mouseMoved(MouseEvent event){}

It looks to me like mouseMoved() is very similar to mouseMove(),  which is what I was using in the first place!

Am I doing this all wrong here?
agreesAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
1) Wrong method, see MouseMotionListener class

2) what you have done is fine

3) Adding the extra methods is no problem, and they must be ikmplemented somewhere.
To avoid defining them you'll need to use a seperate class as your MouseListener, but as I said its not a problem.

0
 
CEHJConnect With a Mentor Commented:
Probably easiest to use an inner adapter class:


yourCanvas.addMouseMotionListener(new YourMouseMotionAdapter());

      // Inner class of canvas

      class YourMouseMotionAdapter extends MouseMotionAdapter {
            public void mouseMoved(MouseEvent e)  {}
      }
      
0
 
objectsCommented:
You can also use processMouseMotionEvent(), in which case you don't need any listeners.
Simply add that method to your subclass.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
EmildaConnect With a Mentor Commented:
See the following code snippet, where c is a Canvas object.

As shown, you can use an abstract adapter, MouseMotionAdapter class for receiving mouse motion events. This class exists as convenience for creating listener objects.  Or you can implement the inferface, MouseMotionListener( but in that case you need to implement all the methods in that inferface. You can leave the unwated methods as empty) and add it as listener to Canvas object.

c.addMouseMotionListener(new MouseMotionAdapter() {
      public void mouseMoved (MouseEvent e) {
                      // do your process here.
      }
    });

or if your class has extended from a Canvas, then you can implement processMouseMotionEvent() method include the necessary actions inside.
0
 
CEHJCommented:
>>3) Adding the extra methods is no problem

It is if you don't want to use them

>>To avoid defining them you'll need to use a seperate class as your MouseListener

(as my example shows)
0
 
agreesAuthor Commented:
>> You can also use processMouseMotionEvent(), in which case you don't need any listeners.

That seems like the most straight forward thing to do, but I tried adding this method but it is never triggered.  Is there something else I need to do?
0
 
objectsCommented:
> It is if you don't want to use them

like I said its no problem :-D

> (as my example shows)

It doesn't need to be an inner class, you can use whatever class you want.

In fact you don't even need to use a seperate class at all.

0
 
CEHJCommented:
>> You can also use processMouseMotionEvent(), in which case you don't need any listeners.

NOT a good idea. You don't need to (and shouldn't) revert to an earlier component model in order to get the functionality you need
0
 
objectsCommented:
> NOT a good idea.

why exactly ???

> You don't need to (and shouldn't) revert to an earlier component model in order to get the functionality you need

if that was the case then surely it would be deprecated


0
 
objectsCommented:
> Is there something else I need to do?

make sure you call enableEvents, eg.

this.enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
0
 
CEHJCommented:
>>why exactly ???

Reasons such as

a. it's error prone (e.g. that code you posted last could easily cause errors with the rest of the program)
b. it produces tight coupling with event listening, which is undesirable in nearly all cases

>>if that was the case then surely it would be deprecated

Not much point deprecating it when it is central to an earlier event model. That doesn't mean the earlier model should be perpetuated in Swing though
0
 
objectsCommented:
Its not error prone at all, it fact it's very stable.

> (e.g. that code you posted last could easily cause errors with the rest of the program)

agrees,

Try it out, and look at the example I posted earlier :)

> That doesn't mean the earlier model should be perpetuated in Swing though

Swings got nothing to do with it
0
 
CEHJCommented:
>>Its not error prone at all, it fact it's very stable.

Stability has nothing to do with it. It's simply error prone. The reason? - agrees, you will have to OR all the constants for all events you want to handle, or you program will simply not work properly
0
 
objectsCommented:
> you will have to OR all the constants for all events you want to handle, or you program will simply not work properly

Of course you would, thats what the method is for :D
Certainly doesn't make it error prone.
0
 
CEHJCommented:
I shall email you privately about the above
0
 
agreesAuthor Commented:
>> make sure you call enableEvents, eg.
>> this.enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);

Doesn't that do the same as adding a listener?

>> CEHJ, I do not see ANYTHING constructive in your comments on the objects

Well disagreement is good but unfortunately I am not qualified to judge whose answer is the best!  So I'll have to split the points if no third party can give further insights.

Currently I am using the inner adapter class solution and it seems to work.
0
 
objectsCommented:
> Doesn't that do the same as adding a listener?

other way around, adding a listener will enable events.
0
 
objectsCommented:
>  I am not qualified to judge whose answer is the best

just explaing the options to you :)
0
 
CEHJCommented:
>>  I am not qualified to judge whose answer is the best

Try them both, you'll soon find out when it comes to writing, maintaining or extending the code (or someone else will ;-))
0
 
CEHJCommented:
:-)
0
 
objectsCommented:
let me know if you need further help in the future :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.