agrees
asked on
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.addMouseMotionListene r(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?
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.addMouseMotionListene
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?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>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)
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)
ASKER
>> 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?
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?
> 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.
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.
>> 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
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
> 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
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
> Is there something else I need to do?
make sure you call enableEvents, eg.
this.enableEvents(AWTEvent .MOUSE_MOT ION_EVENT_ MASK);
make sure you call enableEvents, eg.
this.enableEvents(AWTEvent
>>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
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
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
> (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
>>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
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
> 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.
Of course you would, thats what the method is for :D
Certainly doesn't make it error prone.
I shall email you privately about the above
ASKER
>> make sure you call enableEvents, eg.
>> this.enableEvents(AWTEvent .MOUSE_MOT ION_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.
>> this.enableEvents(AWTEvent
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.
> Doesn't that do the same as adding a listener?
other way around, adding a listener will enable events.
other way around, adding a listener will enable events.
> I am not qualified to judge whose answer is the best
just explaing the options to you :)
just explaing the options to you :)
>> 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 ;-))
Try them both, you'll soon find out when it comes to writing, maintaining or extending the code (or someone else will ;-))
:-)
let me know if you need further help in the future :)
Simply add that method to your subclass.