Over a million developers have joined DZone.

Mini Tutorial on the NetBeans Lookups

DZone's Guide to

Mini Tutorial on the NetBeans Lookups

· Java Zone ·
Free Resource

Atomist automates your software deliver experience. It's how modern teams deliver modern software.


Here's a mini tutorial I made about the usage of the Lookup class to summarize what I've learned so far.

Let's define a few tool classes

class Tool{}
class Screwdriver extends Tool{}
class Hammer extends Tool{}

That makes a lot of tools... Let's put a few of them into a handy bag !

InstanceContent myContent = new InstanceContent();
Lookup myHandyBag = new Lookup(myContent);

myContent.add( new Screwdriver() );
myContent.add( new Hammer() );
myContent.add( new Hammer() );

Let's lookup what is in my bag !

Collection<Tool> allMyTools = myHandyBag.lookupAll(Tool.class);
System.out.println("What tools do I have ?");
for(Tool myTool : allMyTools){
    System.out.println("I've got a "+myTool.getClass().getName());

What if I'm looking for screwdrivers right now, not just any Tool...

Collection<Screwdriver> allMyScrewdrivers = myHandyBag.lookupAll(Screwdriver.class);
System.out.println("What screwdrivers do I have ?");
for(Screwdriver myScrewdriver : allMyScrewdrivers){
    System.out.println("I've got a "+myScrewdriver.getClass().getName());

Great, I can very easily find the content of my bag, and even specific content-only !


But what if someone else puts a tool of his in my bag ? I'd love to know...
So I need someone to watch my bag !
Here's how to put a guy watching my bag. Great, he'll tell me as soon as anybody puts something in the bag !

class GuyWatchingMyBag implements LookupListener{
    public void resultChanged(LookupEvent e){
        System.out.println("Hey, someone has put (or removed) a tool into your bag !");

GuyWatchingMyBag harry = new GuyWatchingMyBag();
Lookup.Result<Tool> bagWatch = myHandyBag.lookupResult(Tool.class);
bagWatch.addLookupListener( harry );

//let's try it !
//[note : it would be better for the tutorial if another class made that, or maybe it could be triggered by a button ? just to show it's external]
myContent.add( new Screwdriver() );
myContent.add( new Hammer() );
//I should get a message in the output ! Great !

I can also have a guy watch only the addition of specific Tool classes !

//let's tell harry to stop watching the bag first
bagSpecificWatch.removeLookupListener( harry );

GuyWatchingMyBag james = new GuyWatchingMyBag();
Lookup.Result<Tool> bagSpecificWatch = myHandyBag.lookupResult(Hammer.class);
bagSpecificWatch.addLookupListener( james );

//Let's try it !
//[note : it would be better for the tutorial if another class made that,  or maybe it could be triggered by a button ? just to show it's external]
myContent.add( new Screwdriver() );
myContent.add( new Hammer() );
//Great ! You have only been alerted that the hammer was added, and haven't been disturbed for the Screwdriver that was added since it did not interest you.

Let's let anybody watch my bag, so if they need tools, they can get them !

public Lookup getLookup(){
    return myHandyBag;
//This is exactly the implementation of a Lookup.Provider interface.


How can this be useful in the problem of modules communication ?

  • There is already a global lookup, which any modules can Listen to.
  • It is called Utilities.actionsGlobalContext();
  • It actually proxies to the Lookup of the currently active TopComponent.
  • TopComponents natively implement the Lookup.Provider interface, which means each TopComponent already has a Lookup.
  • A TopComponent's lookup primary purpose is to hold it's selection. This way, thanks to Utilities.actionsGlobalContext(), any Module can watch the selection of the active TopComponent.

This means that without even knowing what TopComponent is active, you now can listen to it's selection !

So, that's the main point of Lookups.

Continue by reading
NetBeans Selection Management Tutorial I.
The post put me on the right trail (it covers the same as this mini-tutorial and goes a bit further)

Get the open source Atomist Software Delivery Machine and start automating your delivery right there on your own laptop, today!


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}