YourFirstNetbeansModule

Your First Netbeans Plugin

Doc Contributor: Silveira Neto. This tutorial was first published in my blog.


Introduction

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. For accomplish this tutorial all you'll need is Netbeans 6.0 or greater.

First Step: A new module.

Create a new project clicking in File → New Project.

File:Figure 1. Creating a new project

In Categories select Netbeans Module and in Projects select Module.

File: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.

File:Figure 3. Naming the module.

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

File:Figure 4. Package name.

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

Second Step: Some Action.

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

File: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.

File: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.

File: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.

File:Figure 8. Name, Icon and Location.

I used an smile icon from the Pidgin Project.

Image:wink24_YourFirstNetbeansModule.png

After clicked in Finish your project will look like that:

File:Figure 9. Empty action.

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

File:Figure 10. Install/Reload in target platform.

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

File:Figure 11. Look! We got an plugin!


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

Third 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 Image:puzzle_YourFirstNetbeansModule.png HelloYou and go in Properties. Another way to do the same is going in File → “HelloYou” Properties.

File: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.

File: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.

File:Figure 14. Add Modules Dependency

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

File:Figure 15. Dialogs API.

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

File: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: Image:puzzle_YourFirstNetbeansModule.png HelloYou → Install/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.

File: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, Image:puzzle_YourFirstNetbeansModule.pngHelloYou → Install/Reload in Target Platform.

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

Image:figure_YourFirstNetbeansModule.18.png

Image:figure_YourFirstNetbeansModule.19.png

That’s it. You made your first Netbeans plugin!

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