• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hi there I've been stuck for three days now

 
Jonathan Johansen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
{
if(on)
{
for(int dm = 0;dm<buttonList.size();dm++)
{
buttonList.get(dm).setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
buttonList.get(dm).addMouseMotionListener(new DragHandler());
}
}
else if(on==false)
{
for(int dm = 0;dm<buttonList.size();dm++)
{
buttonList.get(dm).setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
buttonList.get(dm).removeMouseMotionListener(new DragHandler());
}

}
}
 
Rob Camick
Ranch Hand
Posts: 2613
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider these lines of code:

ArrayList<String> list = new ArrayList<String>();
list.add("asdf");
list.remove("asdf");
System.out.println(list.size());

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());
System.out.println(list.size());

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
Bartender
Posts: 1306
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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 ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic