File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes ClassCastException while casting to XMLTreeNode(JTree from XML) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "ClassCastException while casting to XMLTreeNode(JTree from XML)" Watch "ClassCastException while casting to XMLTreeNode(JTree from XML)" New topic
Author

ClassCastException while casting to XMLTreeNode(JTree from XML)

Vaijan Hundlekar
Greenhorn

Joined: Mar 23, 2009
Posts: 6
This is the statement on which I am getting ClasscastException:

public void visitAllNodes() {
//System.out.println(tree.getModel().getRoot());
XMLTreeNode root = (XMLTreeNode)tree.getModel().getRoot(); //getting exception
visitAllNodes(root);
}
I am Using XMLTreeModel, XMLTreeNode,XMLTreePanel classes for making a JTree from a XML.
And above method for visiting all nodes, but getting an exception.
I am not able to figure out why this is happening, please help.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

It's happening because the getRoot() method returns something which isn't an XMLTreeNode or a subclass of it. That's by definition what ClassCastException always tells you.
Vaijan Hundlekar
Greenhorn

Joined: Mar 23, 2009
Posts: 6
Thanks Paul.

This is the getRoot() function:
public Object getRoot() {
if (document==null) {
return null;
}
Vector<Element> elements = getChildElements(document);
if (elements.size() > 0) {
return new XMLTreeNode( elements.get(0));
}
else {
return null;
}
}
And i checked it, its not returning null, so wat is the exact problem why it isnt able to cast it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Are you sure this model is being used?
Try the following:

This will tell you exactly of what class both the TreeModel and its root object are.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Vaijan Hundlekar
Greenhorn

Joined: Mar 23, 2009
Posts: 6
hey Rob,
thanks..i tried what you asked for and got the following output:
class com.TestDb.XMLTreeModel
class com.TestDb.XMLTreeNode
Now i have to cast it in XMLTreeNode...


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Ok, so the class should be right. Do you happen to have another XMLTreeNode class somewhere, and have imported that one? If so, you think you're casting to com.TestDb.XMLTreeNode but in fact you may be trying to cast to xxx.yyy.XMLTreeNode.
Vaijan Hundlekar
Greenhorn

Joined: Mar 23, 2009
Posts: 6
Hi Rob that worked!
Now i am having one problem:

Below function is in XMLTreePanel:

This is my XMLTreeNode Class:
package com.TestDb;


can you be able to help me in writing a getPath() function in XMLTreeNode class?


Actually i want to implement a search function which after getting the required string in a tree node it shud scroll to display the node in a JTree.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

First of all, please Use Code Tags. It will preserve your indentation.

I would implement getPath() as follows:
Vaijan Hundlekar
Greenhorn

Joined: Mar 23, 2009
Posts: 6
Hi Rob,
i tried your function but i think node.getParent() is not implemented.
See my aim is to search a node in a JTree and display it to user(select/highlight), and i tried to do it but no success could you please write a search method which will select/highlight/color a node in XMLTreePanel class given below.
i am pasting all the classes here :
XMLTreeNode: pasted in my previous post

XMLTreePanel:



XMLTreeModel:

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Vaijan Hundlekar wrote:i tried your function but i think node.getParent() is not implemented.

Then XMLTreeNode does not implement TreeNode. You will need this method, so if it isn't there add it.

could you please write a search method which will select/highlight/color a node in XMLTreePanel class given below.

No. Just because I have given you the code for a pretty simple method doesn't mean I will do all the work for you.
You should check out the JTree API; JTree.setSelectionPath should do the trick. But for that you need a TreePath.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ClassCastException while casting to XMLTreeNode(JTree from XML)