NetBeans Lookup Example

Lookup example, producing a new message automatically every 2 seconds:

  • Provider module:

    public final class Selection {
        private Selection() {
        private static MyLookup LKP = new MyLookup();
        //Make the Lookup accessible:
        public static Lookup getSelection() {
            return LKP;
        private static final class MyLookup extends ProxyLookup
                implements Runnable {
            private static ScheduledExecutorService EX = 
            public MyLookup() {
                EX.schedule(this, 2000, TimeUnit.MILLISECONDS);
            private int i;
            public void run() {
                //Add to the Lookup a new MyHello:
                setLookups(Lookups.singleton(new MyHello(i++)));
                EX.schedule(this, 2000, TimeUnit.MILLISECONDS);
        private static final class MyHello implements HelloProvider {
            private String text;
            public MyHello(int i) {
                text = i % 2 == 0 ? "Hello from Tom" : "Hello from Jerry";
            public String sayHello() {
                return text;

    public interface HelloProvider {
        public String sayHello();

  • Consumer module, with dependency on the provider module:

    final class HelloTopComponent extends TopComponent implements LookupListener {
        private static HelloTopComponent instance;
        private static final String PREFERRED_ID = "HelloTopComponent";
        private Result result;
        private HelloTopComponent() {
            //We have a dependency on the provider module,
            //where we can access Selection.getSelection():
            Lookup lookup = Selection.getSelection();
            //Get the HelloProvider from the result:
            result = lookup.lookupResult(HelloProvider.class);
            //Add LookupListener on the result:
            //Call result changed:
        StringBuilder sb = new StringBuilder();
        int i;
        public void resultChanged(LookupEvent arg0) {
            long mills = System.currentTimeMillis();
            Collection<? extends HelloProvider> instances = result.allInstances();
            for (HelloProvider helloProvider : instances) {
                String hello = helloProvider.sayHello();
                sb.append(mills + ": " + hello + "\\n");



Hi Geertjan,

at the moment I am really interested in things like servicelocator, dependency injection (DI) and the stuff, too. I only knew spring, but some days ago I discovered this container:
(and guice ...)

I like picocontainer, because it is quite small (<200 KB), there are a lot of interesting features and the authors are quite helpful. If you use on the top you can e.g. configure the container via xml, beanshell, groovy ...

And I have learned that a singleton could be an anti-pattern:
They (from picocontainer) wouldn't use "Lookup Selection.getSelection()" they would use DI:

private HelloTopComponent(Lookup lookup){

But at the moment I don't know if the picocontainer API would provide sth. similar to the result.addLookupListener method. Maybe the start/stop methods there could help, I'll figure it out if you are interested.

If you would like to see my first (stupid) swing app you can visit:

To build it, just copy dist/lib/\* to lib and use netbeans ;-)


PS: Keep on good posting ;-)

Posted by Peter on April 25, 2008 at 06:54 AM PDT #


Posted by guest on April 28, 2008 at 01:44 AM PDT #


thanks for nice example. I found one very weird thing.

When I create MyLookup as public class and try to invoke directly (from EDT) it does not send information into lookup.

When I use new Thread(new Runnable() { ... }).start() to fire then it works.

I don't understand why. Can you clarify it to us?


Posted by Tom on June 09, 2008 at 05:39 PM PDT #

Hi, I an newbie to NetBeans and working to create an application. I have created a login screen with the NetBeans extending ModuleInstall. I want to stop/abort loding main window (other modules) if login is failed. Could you please provide some ideas on that?

Posted by sanjay on May 27, 2013 at 03:15 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« November 2015