Geertjan's Blog

  • May 11, 2005

NetBeans Modules for Dummies (Part 6)

Geertjan Wielenga
Product Manager
In this final installment of the unofficial "NetBeans Modules for Dummies" guide, we're going to apply the principles of yesterday's installment to the Anagrams sample that is distributed with the NetBeans IDE. We'll start with a standard J2SE application and finish with an NBM. The NBM will give us a new menu called "Games" with a submenu called "Anagrams". Clicking on "Anagrams" will pop up the Anagrams sample. Of course, we could choose to install the NBM either in the NetBeans IDE or in the NetBeans Platform. (Maybe we could even make a whole bunch of games, then turn them into NBMs and install them in the NetBeans Platform and end up with our very own Games Center.)

  1. Get the J2SE sample. Choose File > New Project > Samples > General > Anagram Game. Click Next and Finish.
  2. Modulerize the sample. In Anagrams.java, two methods need to be changed: exitMenuItemActionPerformed and exitForm. Instead of closing the system when you close the module (which is the behavior of an application), you just want to dispose of the module when you close it. Hence, replace the content of these two methods with the following:

  3. Create the module's supporting files.

    • The Action class. Right-click the Source Packages node and choose New Java Class. Name your class ShowAnagramsAction and type com.toy.anagrams in the Package field. Click Finish. Replace the code with the following:

      public class ShowAnagramsAction extends CallableSystemAction {
      public HelpCtx getHelpCtx() {
      return HelpCtx.DEFAULT_HELP;
      public String getName() {
      return "Anagrams";
      public void performAction() {
      Anagrams anagrams = new Anagrams();
      anagrams.setBounds(400, 200, 400, 220);

      Click Alt-Shift-F in the Source Editor. You'll get error messages. Right-click the Libraries node, choose Add Library, and create a library that contains three jars: openide.jar, openide-loaders.jar, and nbantext.jar. (We don't need the latter in this step, but it's good to have it available already.) Once you've added these libraries, which you should be able to find in your distribution or in the Update Center (in the case of nbantext.jar), and click Alt-Shift-F again, the error messages disappear.

    • The Layer XML file. Right-click the Source Packages node and create an XML Document called layer. Put it in a package called com.toy.anagrams.resources. Click Finish. Replace the code with the following:

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE filesystem
      PUBLIC "-//NetBeans//DTD Filesystem 1.1//EN"
      <folder name="Menu">
      <folder name="Games">
      <file name="com.toy.anagrams.ShowAnagramsAction.instance"/>

    • The Manifest file. Right-click the Project node and choose New > File/Folder > Other > Empty File. Call the file manifest.mf and click Finish. Replace the code with the following:

      Manifest-Version: 1.0
      OpenIDE-Module: anagrams/1
      OpenIDE-Module-IDE-Dependencies: IDE/1 > 4.0
      OpenIDE-Module-Layer: com/toy/anagrams/resources/layer.xml
      OpenIDE-Module-Specification-Version: 1.0

      Now add the following line to the nbproject/project.properties file:

  4. Build the module.

    • Get the Ant scripts. Add the following two scripts to your build.xml file:

      <target name="create-xml-file-for-module" description="Create XML File.">
      <!-- Create directory for JAR. -->
      <mkdir dir="netbeans/${modules.dir}"/>
      <!-- Create directory for JAR's XML file. -->
      <mkdir dir="netbeans/${modulexml.dir}"/>
      <!-- Copy the JAR (therefore, you must build the JAR first!). -->
      <copy file="${dist.jar}"
      <!-- Create the XML for the JAR (therefore, must have an appropriate manifest.mf first!). -->
      <taskdef name="createmodulexml"
      classpath="${javac.classpath}" />
      <createmodulexml xmldir="netbeans/${modulexml.dir}">
      <enabled dir="netbeans/${cluster.dir}">
      <include name="modules/${module.name}.jar"/>

      <target name="create-nbm" description="Create NBM File.">
      <taskdef name="genlist" classpath="${javac.classpath}"
      <genlist outputfiledir="netbeans/${cluster.dir}" module="modules/${module.name}.jar">
      <fileset dir="netbeans/${cluster.dir}">

      <include name="modules/${module.name}.jar"/>

      <include name="config/Modules/${module.name}.xml"/>
      <taskdef name="makenbm" classpath="${javac.classpath}" classname="org.netbeans.nbbuild.MakeNBM"/>
      <makenbm file="${module.name}.nbm"
    • Define target properties. In part 4 of this series, the strict requirements of some versions of the nbantext.jar were described. So, some of the properties below -- not all -- are only there to prevent build errors occurring (when nb.system.dir isn't set, for example). Define them in the nbproject/project.properties file:

    • Add the Ant scripts to the build process. When you include the little target below, you don't have to run the above two targets yourself. This is because the IDE has a number of targets as placeholders, which you can fill in with whatever you like. This target, for example, is automatically run after a project is built (which means that the JAR is available, which is what you need when creating an NBM). Here it is. Just add it to the build.xml file:

      <target name="-post-jar" depends="create-xml-file-for-module,create-nbm" />

    • Build the project. Right-click the project and choose Build Project. The messages in the Output window should end with something like the following:

      Enabled modules: [anagrams/1]
      Generating information for Auto Update...
      Building jar: C:\\IDEModules\\AnagramGame2\\anagrams.nbm
      BUILD SUCCESSFUL (total time: 0 seconds)

  5. Install the module. Choose Tools > Update Center (either in the IDE or in the Platform), click the radiobutton at the bottom of the dialog box, and browse to your NBM. After some more clicking, your NBM will be installed and in the menubar you will see the following:

    You can re-order the menu and the menu item as discussed in previous blog entries. When you click the menu item, the Anagrams sample appears. (And you are able to close it, because of what you did in step 2 above.) Congratulations.

So now, since you know the basics of NetBeans Modules, you can no longer be called a dummy. Therefore, this series of short tips and articles aimed at NBM beginners has now come to an end. I will blog more about the things I discover in the NBM area, because it's all really interesting and relatively straightforward. Of course, the modules discussed here have been quite simple -- I'd like to find out more complex things such as, for example, how to create a node in the Runtime window, how to write a module to start a server, how to write a template and install it via a module, and the many other magical things the Layer XML file can do for me. But that's also one of the really cool things about NetBeans -- there's always something more to learn...

Join the discussion

Comments ( 3 )
  • Charlie Hunt Wednesday, May 11, 2005
    Thought I would pass this along since others may run into this. It appears to be an issue on Linux and perhaps on Unix too. Likely not an issue for Windows platforms. The module.name in the project.properties file should be spelled with a lower 'a' instead of an uppercase 'A'. That is, the 'module.name' should listed as "module.name=anagrams". If you see a message like "Can't get manifest attributes for module jar file..." when trying to build the plug-in/module. This is likely the problem.
  • Geertjan Wednesday, May 11, 2005
    Thanks for passing this on! Great catch.
  • Sudeep Thursday, May 25, 2006
    I am trying to place a property file in the class path of my project in NetBeans5.0. I am not able to do it
    please advice.
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.