File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Hi there I've been stuck for three days now Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Hi there I Watch "Hi there I New topic

Hi there I've been stuck for three days now

Jonathan Johansen

Joined: Aug 04, 2009
Posts: 8
The idea of this code is the following: when user clicks on Move Tables button ActionListener should be removed and MouseMotionListener
should be added , when the user clicks back on the Move Tables button it should add ActionListener back and remove MouseMotionListener.
I tried removeMouseMotionListener, but it does not work. I checked that the Move Tables button was working properly but still does not work.
Please tell me what Iam missing or what needs tro be done to remove MouseMotionListener

public void addMouseDragHandlerListener(boolean on)
for(int dm = 0;dm<buttonList.size();dm++)
buttonList.get(dm).addMouseMotionListener(new DragHandler());
else if(on==false)
for(int dm = 0;dm<buttonList.size();dm++)
buttonList.get(dm).removeMouseMotionListener(new DragHandler());

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2454
What are we supposed to do with all that code? Its not executable and not fomatted for reading.

Use Code Tags

You can edit your original posting to fix it up (and don't forget to fix the indenting).
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 684
Jonathan Johansen wrote:
Please tell me what Iam missing or what needs tro be done to remove MouseMotionListener

I had a similar problem once. As I recall, in the confusion I had managed to register a listener twice but only removed it once. So it was still generating an event. or something like that.
Bernhard Haeussermann
Ranch Hand

Joined: Aug 05, 2009
Posts: 38
Consider these lines of code:

ArrayList<String> list = new ArrayList<String>();

As we would expect, the last line outputs "0". Now, look at this code:

ArrayList<Object> list = new ArrayList<Object>();
list.add(new Object());
list.remove(new Object());

Here the last line outputs "1". The first object we added is still in the list. Why does that happen?
Well, to answer that, I will first explain why the first lines of code do work. In the second we implicitly create a new string "asdf" and in the third line we create another string "asdf". So we have two different strings with the same character sequences. So how does ArrayList find the correct string to remove? It calls equals(stringToRemove) on all its objects, until it finds an object for which it evaluates to true, and removes it.
Now it is clear why the second example doesn't work as intended - The Object class's equals method simply compares the references of the objects, i.e. it returns the value of this==otherObject.

May this issue apply to your case? Maybe you should override the equals-method in DragHandler to simply return true unconditionally.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 1138

@ Bernhard Haeussermann and Jonathan Johansen
Welcome to code ranch

@ Jonathan Johansen
1. Your post title is not describing your problem at all
2. use code tags
3. use indentations

Bernhard Haeussermann's answer is correct and it applies to your case.
if you dont have the reference of the object you want to remove, then how do you expect to remove it ?

I agree. Here's the better link:

I agree. Here's the link:
subject: Hi there I've been stuck for three days now
It's not a secret anymore!