1

I am new to jtree. I want to get the unique id or value of individual nodes which have same parent.

I tried with valuechanged() method, but i am able to get only the string value of every node.

I want to compare the current selecting node with some unique value of particular node. How can i achieve this?

I think i am making clear.

Is there any possibilites available?

Thanks in advance..

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Babu R
  • 1,025
  • 8
  • 20
  • 40

2 Answers2

3

TreeNode has a getParent() method, you can compare the object reference returned with it with ==.

If you really need an unique id based on object identity, consider System.identityHashCode. See the following question: How do you get the "object reference" of an object in java when toString() and hashCode() have been overridden?

Community
  • 1
  • 1
lbalazscs
  • 17,474
  • 7
  • 42
  • 50
  • But that unique value changed when i run the project again.How to solve my problem? Please help me.... – Babu R Jul 16 '12 at 07:54
  • If you want the value to stay the same after restarting the app, there is no easy way, you have to implement this logic somewhere yourself (for example in the userObject field of DefaultMutableTreeNode). – lbalazscs Jul 18 '12 at 10:26
0

I have been working on setting a unique Id for the DefaultMutableTreeNode. One method is to create a simple CustomUserObject Class which has tow properties, Id and Title. We can then assign the CustomUserObject instance as the Node UserObject property.

To make sure that only the Title is displayed in the Tree structure, override the toString() method in the CustomUserObject Class.

/* CustomUserObjectClass */

public class CustomUserObject implements Serializable {

    private int Id = 0;
    private String Title = null;

    public CustomUserObject(int id, String title) {

        this.Id = id;
        this.Title = title;

    }

    public CustomUserObject() {

    }

    public int getId() {
        return this.Id;
    }

    public String getTitle() {
        return this.Title;
    }

    public void setId(int id) {
        this.Id = id;
    }

    public void setSutTitle(String title) {
        this.sutTitle = title;
    }

    @Override
    public String toString() {
        return this.Title;

    }  

Now to create a new node:

CustomUserObject uObj = new CustomUserObject(1, "My First Node");
DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(uObj);

uObj = childNode.getUserObject();
uObj.getId();
uObj.getTitle();
vat
  • 1