Netbeans: your first plugin

Netbeans have a modular architecture that permits plug and unplug functionalities easily using the Plugin Manager.

It’s also easy create new plugins using Netbeans itself.http://silveiraneto.net/wp-admin/media-upload.php?post_id=705&TB_iframe=true

1º Step: A new module.

Create a New Project clicking in File → New Project.

Netbeans: File → New Project
Figure 1. Creating a new project

In Categories select Netbeans Module and in Projects select Module.

Netbeans New Module
Figure 2. Creating a new module.

Click in Next.

In the next screen you can choose your module name, like HelloYou. Leave the rest with the default values.

Netbeans Module Name and Location
Figure 3. Naming the module.

Now you have to set up the package base name, something like org.yourorg.helloyou.

Basic Module Configuration
Figure 4. Package name.

Now you have an empty module, but we need some action.

2º Step: Some action.

Right click in your source package and click in New → Action.

Netbeans Add Action
Figure 5. Adding some action.

Now we’ll fill another wizard. The first screen is about Action Type. Leave all with the default values (the Always Enabled option) and click Next.

Action Type
Figure 6. Filling the Action Type.

In the GUI Registration screen we can set where we want our action appears as an menu item. We chose Menu file and select Global Toolbar Button. In Toolbar select Build and in Position select Profile Main Project…- HERE. Click Next.

Gui Registration
Figure 7. GUI Registration.

Now we will set up the screen Name, Icon and Location. In Class Name we choose HelloAction. Display Name is how the class will appear in the menu, so we can spaces. You should select an icon in dimensions of 16×16. In the same directory you can put to an icon in 24×24. If your 16×16 icon is named ball.png your 24×24 icon should be ball24.png. Doesn’t matter where is this icon, it’ll be automatically copied to the module directory. Now click Finish.

Netbeans Name Icon and Location.
Figure 8. Name, Icon and Location.

I used an smile icon from the Pidgin Project.

WinkWink24

After clicked in Finish your project will look like that:

Look like that
Figure 9. Empty action.

At this point we can already test our module! For doing that right click in PuzzleHelloYou (puzzle icon) and select Install/Reload in Target Platform. This will open a new instance of Netbeans with your module installed!

Install or reload in target platform
Figure 10. Install/Reload in target platform.

If everything goes alright you will see your icon in the toolbar.

Notice the smile
Figure 11. Look! We got an plugin!

But if you click in the smile nothing happens. We have defined no action at all.

3º Step: Creating dependencies.

A module can have dependencies from others modules. When you try to install it, Netbeans will ask about install their modules dependencies. Is much like an Linux package system.

We can see and set up the dependencies in our module properties screen. You can right click PuzzleHelloYou and go in Properties. Another way to do the same is going in File → “HelloYou” Properties.

Helloyou properties
Figure 12. File → “HelloYou” Properties.

In the Project Properties screen select Libraries in the left side, Categories. There are listed the Java, Netbeans and all Module Dependencies of your module. By default you’ll see Utilities API module dependence.

Netbeans Project Properties
Figure 13. Module Dependences.

Click in the button Add (that one near the module dependencies). You’ll see the Add Module Dependency screen where are listed all Netbeans modules.

Netbeans add modules dependency
Figure 14. Add Modules Dependency

We want add an dependency for the Dialogs API. Type “dialogs” in the filter text field.

Netbeans Dialogs API
Figure 15. Dialogs API.

Select the module Dialogs API and click OK. Now our module have two dependencies: Utilities API and Dialogs API.

More module dependencies
Figure 16. Dialogs API and Utilities API.

Now for the first time, let’s right some code.

In HelloAction.java file there’s a class HelloAction that extends CallableSystemAction (an abstract class). In the method performAction() there’s nothing but:

//TODO implement action body

We will rewrite the performAction method:

public void performAction() {
   String msg = "Hello Netbeans World!";
   int msgType = NotifyDescriptor.INFORMATION_MESSAGE;
   NotifyDescriptor d = new NotifyDescriptor.Message(msg, msgType);
   DialogDisplayer.getDefault().notify(d);
}

You will see some warnings (cannot find symbol). Just fix the imports (Ctrl+Shift+I). To test your module, do it again: PuzzleHelloYouInstall/Reload in Target Platform. A new instance of Netbeans will open and you’ll see our smile face button again. When you click it, Netbeans will show an dialog.

Netbeans Hello World
Figure 17. Hello Netbeans World.

Let’s do more. Let’s talk with the user using NotifyDescriptor.InputLine. Will create an input line dialog, get the user name (if he clicks in Ok) and display a message to him.

public void performAction() {
    NotifyDescriptor.InputLine question;
    question = new NotifyDescriptor.InputLine("Name:",
        "What's your name?",
        NotifyDescriptor.OK_CANCEL_OPTION,
        NotifyDescriptor.QUESTION_MESSAGE);
 
    if (DialogDisplayer.getDefault().notify(question) == NotifyDescriptor.OK_OPTION) {
         String msg = "Hello "+question.getInputText()+"!";
         int msgType = NotifyDescriptor.INFORMATION_MESSAGE;
         NotifyDescriptor d = new NotifyDescriptor.Message(msg, msgType);
         DialogDisplayer.getDefault().notify(d);
     }
}

Again, PuzzleHelloYouInstall/Reload in Target Platform.

After the new Netbeans instance opens, click in our smile face.

Type your name

Hello Silveira

That’s it. You made your Netbeans plugin!

Learn more about Netbeans Module Development:

added in 02.27.08: now this tutorial is also available in the Netbeans Wiki.

23 thoughts on “Netbeans: your first plugin

  1. This is interesting, well, in my first tech talk I show how to create a module using swing and NetBeans platform. But now I’m going to use this to.

    Great Job you are doing.
    Saludos
    rik

  2. Good article, It looks that making netbeans plugin become easier after some years :-)
    May I ask which Operating system you were in when you made the screenshots? GUI looks very nice and soft.

    Thanks

  3. Nice Walkthrough on how to create a Netbeans plugin!
    I think I’m gonna try it one of these days.

    Thanks for that great material and keep going with that good work! :)

  4. Wow, this is the program James Gosling thinks makes Emacs obsolete? In Emacs, this whole tutorial can be summed up as:

    1. Add the following text to your .emacs file:

    (defun hello-you (name) (interactive “MWhat’s your name? “)
    (message “Hello %s!” name))

    2. Run your plugin with “M-x hello-you”. Or bind it to your shortcut key of choice. Or a toolbar button, if you’re really allergic to typing.

    Man, I’m so glad we’ve got away from that 1970s junk. All these dialog boxes are so much more productive.

  5. Hi,

    Great news on the Performance Enhancements in NB6.1.

    What happened to the DocBook plugin for NetBeans ? I really would like it at the moment !

    Regards,
    Edward.

  6. Great work!
    althi just wanted to ask…
    wt do i do if i wanted a link to my proj wth this icon tat we created?..how to go bout if i want to write any code,,like for creating a DB conn,,or adding stuff to the pallet

  7. Great!! Thanks Silveira great tutorial,
    nicely done and easy to understand.
    I think ill try my hand at creating one soon..
    Thanks

  8. Silveira hello I read your article, but I have some doubts about the dependencies between various modules that are being developed, I have a module that requires from another module classes I am creating, I have some errors when I compile Due to this.
    How can i resolve this dependency problem

  9. Silveira hello I read your article, but I have some doubts about the dependencies between various modules that are being developed, I have a module that requires from another module classes I am creating, I have some errors when I compile Due to this.
    How can i resolve this dependency problem
    thanks a lot!!!

  10. It was quite useful for me, I didn’t know anything about modules and now I’m familiar with them.
    Thanks Silveira.
    but I still have some problems with dependencies between modules.
    I have 3 modules which module A depends on B, and module B depends on C.
    but I don’t know how to tell netbeans this dependency chain…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>