-
Notifications
You must be signed in to change notification settings - Fork 1
DevFaqNodesChildFactory
If you have a Node that needs to provide child Nodes, and computing the objects the child nodes represent is slow or expensive (i.e. you need to parse a file, connect to a database, or do some sort of I/O), you do not want to compute the child nodes in the event thread (which is what happens by default).
NetBeans 6.0 introduces org.openide.nodes.ChildFactory
and Children.create(ChildFactory factory, boolean asynchronous)
. Simply subclass ChildFactory
and implement protected boolean createKeys(List <T> toPopulate)
to build the list of objects that will be represented by the child nodes. Implement protected Node createNodeForKey(T key)
to create a Node - it will be passed each object in the list of child objects. createKeys
will be called on a background thread.
Typically you’ll want to make the model object from createKeys
available on the Node you create. So a simple implementation of createNodeForKey
would look something like:
protected Node createNodeForKey(T key) {
AbstractNode result = new AbstractNode (Children.LEAF, Lookups.singleton (key));
result.setDisplayName (key.getName()); //or whatever
result.setIcon (Utilities.loadImage ("path/in/jar/to/image.gif"));
return result;
}
ChildFactory can also simplify creating Nodes synchronously, and has the convenience that by using generics, your code can be type safe with respect to key objects. Generally it can be used anywhere Children.Keys
would be used (it uses Children.Keys
under the hood).
The content in this page was kindly donated by Oracle Corp. to the Apache Software Foundation.
This page was exported from http://wiki.netbeans.org/DevFaqNodesChildFactory , that was last modified by NetBeans user Jtulach on 2010-07-24T18:45:00Z.
NOTE: This document was automatically converted to the AsciiDoc format on 2018-01-26, and needs to be reviewed.
Apache NetBeans is an effort undergoing incubation at The Apache Software Foundation (ASF).
Incubation is required of all newly accepted projects until a further review indicates that the infrastructure, communications, and decision making process have stabilized in a manner consistent with other successful ASF projects.
While incubation status is not necessarily a reflection of the completeness or stability of the code, it does indicate that the project has yet to be fully endorsed by the ASF.
This wiki is an experiment pending Apache NetBeans Community approval.