Over a million developers have joined DZone.

How to Create & Import a JavaBean Component in NetBeans IDE (Part 2)

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

In 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".

New JFrame

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.

New JFrame

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".

New JFrame

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.

New JFrame

Next, in the resulting file chooser dialog, navigate to the location where the JAR file containing your JavaBean is stored, and click "Next".

New JFrame

From the list of available components choose your Component and click "Next".

New JFrame

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".

New JFrame

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.

New JFrame

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

New JFrame

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.

New JFrame

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.

New JFrame

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.

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}