tags:

views:

65

answers:

3

Hello. I dont know how to acces my method of my class ProjectNode, that is returned from ExplorerManager mgr like this:

mgr.getRootContext().setSomething()

getRootContext() returns Node object, but I put class ProjectNode (extends AbstractNode, abstractNode extends Node)into rootContext.

The compiler does not want to eat that line of code. But it must!

+3  A: 

If getRootContext() returns a Node, then you can only call the methods defined in Node, not in its subclasses. You can cast the return value to another class if you need that:

Node rootContext = mgr.getRootContext();
if(rootContext instanceof ProjectNode){
    ProjectNode rootProjectNode = (ProjectNode)rootContext;
    rootProjectNode.setSomething();
} else {
    //handle this case
}
Thomas Lötzer
+1  A: 
((ProjectNode)mgr.getRootContext()).setSomething();

don't forget to check type!

Andrey
it works, thanks
badgirl
A: 

IfsetSomething() is not a public method on Node class, you can not "feed the compiler" that code - no matter how you try. As all the wise men has spoken above, you must cast the result to the subclass that defines your setSomething() method.

ring bearer