1

I have the following code

TreePath tp = jTree.getSelectionPath();
String path = tp.getPath().toString();

This will set path as Ljava.lang.Object;@33530691, I understand this is because

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' +Integer.toHexString(hashCode())

But how do you actually get this value as a String?

9 Answers 9

1

getPath() returns an array of String values representing the components in the tree. If you need the String representation of the root, you will do getPath()[0];

Sign up to request clarification or add additional context in comments.

1 Comment

Thought .getPath returned Object and not Object[], should have payed more attention!!
0

String's toString() method is by defalut overloaded, so the problem is not here with the String path, but try to see the TreePath tp.

I think you need to take a look at tp.

Comments

0

I don't quite get your question. Isn't that already answered in the quote you posted?

getClass().getName() + '@' +Integer.toHexString(hashCode()) the default implementation of the toString() method, but you can override that, if you need a better representation (keep in mind that you need to extend TreePath in your case ).

Edit: I think, I now understand:

Since TreePath.getPath() returns an object array you get that java.lang.Object in the output.

Comments

0

The object that is the component of the tree needs to have its toString() method overridden. You can override it with whatever you what it to return. I presume you are adding a custom object to the tree so you are in control of that object and its toString() method

Comments

0

If you want to get a comma separated string of the values you can do

String path = new ArrayList(tp.getPath()).toString();

Comments

0

Try something like this:

Object[] treeNodes = tp.getPath();
StringBuilder sb = new StringBuilder();
for(Object tn : treeNodes) {
    sb.append(File.separatorChar).append(tn.toString());
}
String path = sb.toString();

Comments

0

You have an array of objects (which may all be Strings)

To toString these you need to use Arrays.toString()

String path = Arrays.toString(tp.getPath());

Comments

0

If it's for output/logging only, I guess Arrays.asList(tp.getPath()).toString() could suffice.

Comments

0

The TreePath object represent the whole path to go from the root-node to the actual node. If you call getPath on it, it returns you an array containing the root-node and the actual selected node, and all nodes in between.

How to get the "correct String" depends on what you actually want to achieve:

  • If you want the string representation of the actual selected node, use the getLastPathComponent method

    TreePath tp = jTree.getSelectionPath();
    String path = tp.getLastPathComponent().toString();
    
  • If you want to have the complete path, you will have to loop over the array

    TreePath tp = jTree.getSelectionPath();
    StringBuilder pathBuilder = new StringBuilder();
    for( Object node : tp.getPath() ){
      pathBuilder.append( node.toString() );
      //possibly append a separator as well
    }
    String path = pathBuilder.toString();
    

    or simply use the Arrays#toString method on tp.getPath() instead of the toString method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.