Category Archives: Netbeans

Reflection problem in the Netbeans Nodes API tutorial

I’ve been following the Netbeans tutorial on using the Nodes API to implement a tree structure with some information in it.

In the second part of the tutorial the author shows how to set up and show a property sheet sheet when an element in the tree structure is selected. Most of the examples work pretty straight forward, but when I tried to adapt the example to my own code, I got a problem with the way the tutorial uses the Java reflection to set the fields of the property sheet. Here is the line in the tutorial:

APIObject obj = getLookup().lookup(APIObject.class);
Property indexProp = new PropertySupport.Reflection(obj, Integer.class, "getIndex", null);

When ever I try to do that with my own implementation of an APIObject, I get a NoSuchMethodException. After trying to work out why that was happening, without any success, I figured another way to create the property sheet, based on this blog post: http://blogs.oracle.com/geertjan/entry/connecting_shapes_showing_properties by Geertjan Wielenga from the Netbeans group.

The code becomes like this instead:

APIObject obj = getLookup().lookup(APIObject.class);
Property indexProp = new PropertySupport.ReadOnly("objIndex" Integer.class,"Index","Object index") { 

@Override
public Integer getValue() throws IllegalAccessException, InvocationTargetException {
 return obj.getIndex();
}
};

While that is not really a solution to the non-working reflection, it is still a workaround that can be used.