NetBeans Platform Cookbook Chapter 03 05

Component Communication, Selection

Each application keeps its internal state. It knows which component is active (has focus). The selected object is passed from the list to the Detail component. Actions are enabled if the corresponding data type is used. Simply spoken modules have to be independent but they must communicate together.


NetBeans Platform provides a special Lookup instance - Actions Global Context - that keeps the context for performing actions. Here important active object instances are stored which actions can use - active (selected) Node, TopComponent, ActionMap etc. It is accessible by Utilities.actionsGlobalContext() method. The standard implementation is provided by Windows module API. It observes what TopComponent is active and if it contains any active (selected) Node. You can even listen when the content changes.

All interesting objects (collected from found Lookups) are added into the Action Global Context Lookup.

So - if you want expose something to use for action store it into object's Lookup or provide any activated Node - selected Node in the Explorer component or set activated Node in the TopComponent.

How to

Imagine you will show the list of books. Double-click on the book node opens it in the editor. The Book has next properties: title, authors, year and publisher.

Create new suite project (communication) and its new module.

Create new Explorer TopComponent. How to create an Explorer window is described in the chapter 6 Nodes and Explorer (the description can be included into this one). Create BookListChildren under the root Node and BookNode class.

Store the Book instance into the Lookup of the BookNode.

public class BookNode extends AbstractNode {
    private static Action [] actions = new Action[] { new OpenAction() };
    public BookNode(Book key) {
        super(Children.LEAF, Lookups.fixed(key) ); = key;

You see the Book is stored into Lookup of the superclass. Who knows the Book class and works with it asks the selected Node (activated Node) for Book instance not by

selectedNode.getBook()	;   // wrong way

but by

selectedNode.getLookup().lookup(Book.class);  // the right way

Create DetailTopComponent and implement to show Book information by showBook(Book book, int count) method.

Add Lookup.Result<Book> and a LookupListener as a member

    private Lookup.Result<Book> bookResult;
    private LookupListener bookResultListener;

and initialize them in the constructor.

        bookResult = Utilities.actionsGlobalContext().
                                                    lookupResult( Book.class );
        bookResultListener = new BookResultListener();

The Result contains Book instances in the Actions Global Context Lookup and the listener is informed about changes of this list.

Implement the BookResultListener to show selected Book(s).

   protected class BookResultListener implements LookupListener {
        public void resultChanged(LookupEvent ev) {
            Collection<? extends Book> books = bookResult.allInstances();
            if (books.isEmpty()) {
                showBook(null, 0);
            else {
                Book b = books.iterator().next();
                showBook( b, books.size() );  
    } // BookResultListener

I pass number of selected books to indicate that more books are selected.

Run the application. If you select any Book node the ExplorerManager exposes the selected Node as active Nodes into Actions Global Context Lookup. Its content is observed by DetailTopComponent and the selected Book is shown.


Figure 3.7 Component communication.

Notes/Tips - see to Properties

The same principle uses the Properties Window. It is listening to Action Global Context if any Node is selected. If yes it asks the Node for Property Sheet (by method createSheet(), it is not stored in the Node's Lookup). If returned Sheet is not empty the Properties Window shows properties.

The Window System API observes which TopComponent or Node is active, looks up for ActionMap or actions and builds context menu or enables context sensitive actions.


Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo