silveira neto

carbon-based lifeform. virgo supercluster

Menu Close

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.

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.


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 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);

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?",
    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);

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.

© 2016 silveira neto. All rights reserved.

Theme by Anders Norén.