Skip to main content

Deliver your beans in a NetBeans module (the hard way)

Posted by timboudreau on April 5, 2006 at 5:13 PM PDT

In Monday's blog, I announced a webstart-enabled standalone tool that will generate a NetBeans plug-in module that takes a JAR of Swing components, putting them on the component palette for users to drag and drop. Here I describe how to do the same thing using the plug-in development tools inside NetBeans.

I had actually written this up a couple of weeks ago, and it was my friend and colleague Trung Duc Tran who said "there should be a tool to do that." He was right. So I held this blog and wrote BeanNetter.

Nonetheless, the step-by-step here is worth posting, since you can see what's being generated by the tool under the hood, and it's useful if you're making a module already. So here's how you'd do the same things by hand:

  1. The first thing you want to do is set your JavaBean up as a Library in NetBeans - that's a library that can be shared among projects you're working on. Go to Tools | Library Manager, and add your component's JAR as a library you'd use in normal development - you'll see why in a minute
  2. The second thing you want to do is create a plug-in project. That's easy and it's built right into NetBeans. Just go to New | New Project, and choose NetBeans Plug-In Modules | Module Project
  3. On the next screen, provide a name - short and clear is usually good, same name as your component would also make sense
  4. For code name base, just use the fully qualified name of your component. Don't worry about it too much, it just needs to be unique.
  5. For module display name, type in some nice human readable name for your component(s)
  6. Your project will open, and you'll have a package with the same name as the code name you entered. Select the package and choose New | File
  7. Select NetBeans Module Development | J2SE Library Descriptor. What's that?

    Well, NetBeans already has a way to embed "libraries" in a module and have them show up in the list of libraries a user can use. So the only thing we're going to need to do in addition is add a little metadata to put the bean(s) on the Component Palette

  8. Click next and finish. You'll see an XML file is created. Don't worry about it, we're not going to need to touch that.
  9. Now we get to add a little bit of metadata that puts an entry on the component palette.
  10. Select your package again and choose File | New, and in the new file dialog, create a new empty XML file. Call it something like MyBeanPaletteItem. Paste the following content into it, substituting the fully qualified name of one JavaBean and the name of the library:


     
     
         
     

  11. There's only one step left - open the layer.xml file in your package. Add the highlighted portion you see below, substituting the palette item xml file you just created for the one listed here.



       
           
               
           

       


       
           
               
           

       


  12. Right click the project and choose Create NBM. When it's done, you've got a NetBeans module that will auto-install your JavaBean into a user's component palette. Try it out on an instance of NetBeans that knows nothing about your library - just start NetBeans from the command line, e.g. nb -userdir /tmp/foo. When it comes up, choose Tools | Update Center. Select the Install Manually Downloaded Modules option, and find the NBM file that was created (hint: look in the output window of the instance of NetBeans you built it in).

And poof, you've got a NetBeans plugin that delivers your components right onto the component palette of any NetBeans 5.0 user.

If you have some problems, this usually means one of the strings in one of the XML files is not correct. Try starting the instance of NetBeans you install the module in, from the command line, and add the line switch -J-Dnetbeans.logger.console=true to get an idea of where the problem might be.