How to Create & Import a JavaBean Component in NetBeans IDE (Part 2)
Join the DZone community and get the full member experience.
Join For FreeIn the final installment of this two part tutorial (part 1 here), we look at how to import and use your new JavaBean component in the NetBeans GUI Builder Matisse.
Adding a New JFrame
Once you have created your JavaBean, you are ready to import it into NetBeans. To be able to use it once it is imported however, we first need to create a new JFrame to house the Component in.
For the benefit of this example, in my case I have a project called PDFHelpBean. As shown in the screen shot below, to add a new JFrame to the project, right click on the "Source Packages" and from the resulting pop-up menu select "New" -> "JFrame Form".
NetBeans will now ask you to choose a name for this new Form. In this example it will be called "PDFHelpFrame". Once you have entered the name click "Finish" and NetBeans will generate a new JFrame class.

Importing the JavaBean component
Once you have added a new JFrame, you are ready to add the JavaBean component to the Matisse palette. From the "Tools" menu select "Palette" -> "Swing/AWT Components".
This will bring up a dialog box asking you from where you wish to import your JavaBean. In this case select the "Add from JAR" button.
Next, in the resulting file chooser dialog, navigate to the location where the JAR file containing your JavaBean is stored, and click "Next".
From the list of available components choose your Component and click "Next".
When choosing which category to add the Component to you are free to choose any you like. For the sake of this tutorial however, "Swing Controls" is selected. When you have chosen your category, click "Finish".
When the Palette Manager reappears click "Close".
Adding the JavaBean to Your JFrame
Once you've imported your JavaBean, you should see your component in the Palette. In my case I can see that the PdfHelpPanel component has been added.
You are now ready to add the component to your form. This is done by dragging the JavaBean from the Palette onto your JFrame. As you will see from the screen shot below, once the component has been added to the JFrame, the JavaBean properties will be exposed and available for the end-user to set. In my case (as indicated by the blue arrow) only the fileLocations property is visible
Once the end-user has set any properties they want to, they can run the application. This is done by clicking the green "Run Main Project" button - as indicated by the blue arrow in the screen shot below - in the NetBeans toolbar.
Upon running the application you will see your JavaBean running inside a fully functioning application. In my case PDF Help is shown in the screen shot below.
And thats all there is to it. We hope you have found the tutorial useful. You can see our finished PDFHelp JavaBean application at http://www.idrsolutions.com/help.php.
Opinions expressed by DZone contributors are their own.
Comments