views:

218

answers:

3

Is it possible to convert a String variable to a DefaultMutableTreeNode object? Please explain. Context:

   String s = new String(outputTagName);
Object s2 = (Object) s;
DefaultMutableTreeNode selectedNode2 =(DefaultMutableTreeNode) s2;
DefaultMutableTreeNode parent2 =(DefaultMutableTreeNode) parent;
model.insertNodeInto(selectedNode2, parent2, parent2.getChildCount());

This is the code, I wrote. This is used within an enumeration that traverses the tree in the BreadthFirstSearch fashion. And the 2nf line gives me this error:

java.lang.ClassCastException: java.lang.String cannot be cast to javax.swing.tree.DefaultMutableTreeNode at ....
A: 

That depends entirely on the semantics of the tree you wish to use the node with.

For example, you can create a node containing the string as simply as:

String s = ...; // your string
new DefaultMutableTreeNode(s);

Whether that will be any use to you depends on how you're using the tree.

Basically, more context please. Otherwise this simple "yes" answer is the best you're going to get. :-p

Andrzej Doyle
What would be the semantics , that you speak of here?Can I have more than a yes,pretty please? :P
fixxxer
@fixxxer: What I mean is that the process of "conversion" your question refers to, depends on what properties you want the resulting node to have - which depends in turn on how you use it. So my answer will create a node that wraps the given String, but if the other nodes contained `Booleans` or `Files` or `MyCustomRunnableImpl` (looked up by string ID) this wouldn't do you any good. Basically, what I wrote will work in many situations that your question could describe, and fail in many more that it could describe.
Andrzej Doyle
In my experience, I hv been unable to typecaste the String object to DefaultMutableTreeMode, as the program throws errors(I was running it in Elcipse IDE).So I used setUserObject method to attach the string to the node.But this is just a temporary work-around,in my case.
fixxxer
A: 

You cannot cast String object to any type other than String or Object, or one of interfaces String implements (Serializable, CharSequence, Comparable). To cast object to some type, the object must be of this type. String object is of type String and not of type DefaultMutableTreeNode. You cannot even make DefaultMutableTreeNode a subclass of String (in which case casting would be possible) as String class is final.

Tadeusz Kopec
Can I convert a String to Object type and then convert Object to DefaultMutableTreeNode type?
fixxxer
A String is always a Object, but an Object is not always a DefaultMutableTreeNode
Nettogrof
@Nettogrof What do you think about the ANSWER below this comment?
fixxxer
A: 

Link that you have provide in your other question

You can see that you can do new DefaultMutableTreeNode("Any String");

To correct your code example , the way to do it is:

String s = new String(outputTagName);

DefaultMutableTreeNode selectedNode2 = new DefaultMutableTreeNode(s2);
Nettogrof