Results 1 to 2 of 2
- 12-23-2010, 11:31 PM #1
Member
- Join Date
- Jul 2010
- Location
- Lima, Peru
- Posts
- 47
- Rep Power
- 0
equals() failing in userObject of TreeNode
I'm not exactly sure how to ask this question, since my code is very long and has many pieces and I have no idea where the problem is.
I have a drag and drop implementation that usually works fine (I am using a JTree);
However, with this particular program it is doing the following:
When I drag a node to itself, it is not recognizing it as the same node.
I did some testing and it seems like it is the userObjects of the target drop and the source (which are the same node) are being seen as different. This doesn't make sense because this single node has one single userObject not two different ones. I checked for the hashcodes of the userobjects and they are different.
I am using a drag and drop implementation from another forum I don't know if I should post it here, since it is too long and I'm not sure the problem lies there, so perhaps something is wrong with my program. I don't know where to start though?
My program saves the data of a JTree into a text file and then loads it. The userObject is a custom made one with two strings in it. I attempted trying to override the equals() method but I'm not sure I was successful with that, especially since I don't know much about hashCodes etc.
Any pointers on where I should look to solve this problem would be appreciated.
PSLast edited by PrinceSendai; 12-23-2010 at 11:34 PM.
- 12-25-2010, 08:57 PM #2
Member
- Join Date
- Dec 2010
- Posts
- 20
- Rep Power
- 0
Hello,
Your object is a custom made object existing out of two Strings. Then why do you override the equals method?! The default equals() method will perform (return obj1 == obj2), which means it can compare primitive types only! Since your object is only String you do not need to override the equals() method.
If for some other reason you should override the equals() method, you also need to override the hashCode() method. (Keep in mind that you best not compare objects by comparing the hashCode!)
A perfect equals() method should start with these lines:
Overriding the hashCode() method can be a bit trickier, but doensn't your IDE offers you a solution?Java Code:public boolean equals(Object obj){ if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; // add your own conditions here }
Similar Threads
-
Trying to use RSA encryption, failing...
By soccermiles in forum New To JavaReplies: 2Last Post: 04-26-2010, 07:53 PM -
I tried debug failing: trying to include xml file contents to db table...
By lse123 in forum XMLReplies: 0Last Post: 02-12-2010, 03:33 PM -
Issues while failing to register ttf fonts.
By pvn_myne in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 08-19-2009, 08:36 AM -
[SOLVED] ActionEvent.setSource() is failing in Swing while working in AWT
By playwin2 in forum AWT / SwingReplies: 8Last Post: 08-27-2008, 11:48 PM -
name clash: equals(E) in and equals(java.lang.Object)
By AdRock in forum New To JavaReplies: 0Last Post: 01-25-2008, 11:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks