Results 1 to 2 of 2
  1. #1
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Default 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.

    PS
    Last edited by PrinceSendai; 12-23-2010 at 11:34 PM.

  2. #2
    fex
    fex is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    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:
    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
    }
    Overriding the hashCode() method can be a bit trickier, but doensn't your IDE offers you a solution?

Similar Threads

  1. Trying to use RSA encryption, failing...
    By soccermiles in forum New To Java
    Replies: 2
    Last Post: 04-26-2010, 07:53 PM
  2. Replies: 0
    Last Post: 02-12-2010, 03:33 PM
  3. Issues while failing to register ttf fonts.
    By pvn_myne in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 08-19-2009, 08:36 AM
  4. Replies: 8
    Last Post: 08-27-2008, 11:48 PM
  5. Replies: 0
    Last Post: 01-25-2008, 11:13 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •