Quick Start

«« Previous
Next »»

This lesson describes how to use NetBeans to build a simple application. With a good tool like NetBeans, you can assemble JavaBeans components into an application without having to write any code.

The first three pages of this lesson show how to create a simple application using graphic beans that are part of the Java platform. The last page demonstrates how easy it is to incorporate a third-party bean into your application.

◈ Creating a Project describes the steps for setting up a new project in NetBeans.
◈ A Button is a Bean shows how to add a bean to the application's user interface and describes properties and events.
◈ Wiring the Application covers using NetBeans to respond to bean events in your application.
◈ Using a Third-Party Bean show how easy it is to add a new bean to the palette and use it in your application.

JavaBeans, Oracle Java Certifications, Oracle Java Guides, Oracle Java Learning

Creating a Project


The easiest way to learn about JavaBeans is to start using them. To begin, download and install the latest version of NetBeans. This tutorial describes how to use NetBeans version 7.0.

NetBeans is a bean builder tool, which means it recognizes JavaBeans components (beans) and enables you to snap components together into an application with ease.

A Button is a Bean

Start NetBeans. Choose File > New Project... from the menu.

JavaBeans, Oracle Java Certifications, Oracle Java Guides, Oracle Java Learning

Click for full image
Select Java from the Categories list and select Java Application from the Projects list. Click Next >.

JavaBeans, Oracle Java Certifications, Oracle Java Guides, Oracle Java Learning

Enter SnapApp as the application name. Uncheck Create Main Class and click Finish. NetBeans creates the new project and you can see it in NetBeans' Projects pane:

JavaBeans, Oracle Java Certifications, Oracle Java Guides, Oracle Java Learning

Control-click on the SnapApp project and choose New > JFrame Form... from the popup menu.

JavaBeans, Oracle Java Certifications, Oracle Java Guides, Oracle Java Learning

Fill in SnapFrame for the class name and snapapp as the package. Click Finish. NetBeans creates the new class and shows its visual designer:

JavaBeans, Oracle Java Certifications, Oracle Java Guides, Oracle Java Learning

In the Projects pane on the left, you can see the newly created SnapFrame class. In the center of the screen is the NetBeans visual designer. On the right side is the Palette, which contains all the components you can add to the frame in the visual designer.

A Button is a Bean


Take a closer look at the Palette. All of the components listed are beans. The components are grouped by function. Scroll to find the Swing Controls group, then click on Button and drag it over into the visual designer. The button is a bean!

JavaBeans, Oracle Java Certifications, Oracle Java Guides, Oracle Java Learning

Under the palette on the right side of NetBeans is an inspector pane that you can use to examine and manipulate the button. Try closing the output window at the bottom to give the inspector pane more space.

JavaBeans, Oracle Java Certifications, Oracle Java Guides, Oracle Java Learning

Properties

The properties of a bean are the things you can change that affect its appearance or internal state. For the button in this example, the properties include the foreground color, the font, and the text that appears on the button. The properties are shown in two groups. Properties lists the most frequently used properties, while Other Properties shows less commonly used properties.

Go ahead and edit the button's properties. For some properties, you can type values directly into the table. For others, click on the ... button to edit the value. For example, click on ... to the right of the foreground property. A color chooser dialog pops up and you can choose a new color for the foreground text on the button. Try some other properties to see what happens. Notice you are not writing any code.

Events

Beans can also fire events. Click on the Events button in the bean properties pane. You'll see a list of every event that the button is capable of firing.

JavaBeans, Oracle Java Certifications, Oracle Java Guides, Oracle Java Learning

You can use NetBeans to hook up beans using their events and properties. To see how this works, drag a Label out of the palette into the visual designer for SnapFrame.

JavaBeans, Oracle Java Certifications, Oracle Java Guides, Oracle Java Learning

Edit the label's properties until it looks just perfect.

Wiring the Application


To wire the button and the label together, click on the Connection Mode button in the visual designer toolbar.

JavaBeans, Oracle Java Certifications, Oracle Java Guides, Oracle Java Learning

Click on the button in the SnapFrame form. NetBeans outlines the button in red to show that it is the component that will be generating an event.

JavaBeans, Oracle Java Certifications, Oracle Java Guides, Oracle Java Learning

Click on the label. NetBeans' Connection Wizard pops up. First you will choose the event you wish to respond to. For the button, this is the action event. Click on the + next to action and select actionPerformed. Click Next >.

JavaBeans, Oracle Java Certifications, Oracle Java Guides, Oracle Java Learning

Now you get to choose what happens when the button fires its action event. The Connection Wizard lists all the properites in the label bean. Select text in the list and click Next.

JavaBeans, Oracle Java Certifications, Oracle Java Guides, Oracle Java Learning

In the final screen of the Connection Wizard, fill in the value you wish to set for the text property. Click on Value, then type You pressed the button! or something just as eloquent. Click Finish.

JavaBeans, Oracle Java Certifications, Oracle Java Guides, Oracle Java Learning

NetBeans wires the components together and shows you its handiwork in the source code editor.

JavaBeans, Oracle Java Certifications, Oracle Java Guides, Oracle Java Learning

Click on the Design button in the source code toolbar to return to the UI designer. Click Run Main Project or press F6 to build and run your project.

JavaBeans, Oracle Java Certifications, Oracle Java Guides, Oracle Java Learning

NetBeans builds and runs the project. It asks you to identify the main class, which is SnapFrame. When the application window pops up, click on the button. You'll see your immortal prose in the label.

JavaBeans, Oracle Java Certifications, Oracle Java Guides, Oracle Java Learning

Notice that you did not write any code. This is the real power of JavaBeans — with a good builder tool like NetBeans, you can quickly wire together components to create a running application.

Using a Third-Party Bean

Almost any code can be packaged as a bean. The beans you have seen so far are all visual beans, but beans can provide functionality without having a visible component.

The power of JavaBeans is that you can use software components without having to write them or understand their implementation.

This page describes how you can add a JavaBean to your application and take advantage of its functionality.

Adding a Bean to the NetBeans Palette

Download an example JavaBean component, BumperSticker. Beans are distributed as JAR files. Save the file somewhere on your computer. BumperSticker is graphic component and exposes one method, go(), that kicks off an animation.

To add BumperSticker to the NetBeans palette, choose Tools > Palette > Swing/AWT Components from the NetBeans menu.

JavaBeans, Oracle Java Certifications, Oracle Java Guides, Oracle Java Learning

Click on the Add from JAR... button. NetBeans asks you to locate the JAR file that contains the beans you wish to add to the palette. Locate the file you just downloaded and click Next.

JavaBeans, Oracle Java Certifications, Oracle Java Guides, Oracle Java Learning

NetBeans shows a list of the classes in the JAR file. Choose the ones you wish you add to the palette. In this case, select BumperSticker and click Next.

JavaBeans, Oracle Java Certifications, Oracle Java Guides, Oracle Java Learning

Finally, NetBeans needs to know which section of the palette will receive the new beans. Choose Beans and click Finish.

JavaBeans, Oracle Java Certifications, Oracle Java Guides, Oracle Java Learning

Click Close to make the Palette Manager window go away. Now take a look in the palette. BumperSticker is there in the Beans section.

Using Your New JavaBean

Go ahead and drag BumperSticker out of the palette and into your form.

JavaBeans, Oracle Java Certifications, Oracle Java Guides, Oracle Java Learning

You can work with the BumperSticker instance just as you would work with any other bean. To see this in action, drag another button out into the form. This button will kick off the BumperSticker's animation.

JavaBeans, Oracle Java Certifications, Oracle Java Guides, Oracle Java Learning

Wire the button to the BumperSticker bean, just as you already wired the first button to the text field.

1. Begin by clicking on the Connection Mode button.
2. Click on the second button. NetBeans gives it a red outline.
3. Click on the BumperSticker component. The Connection Wizard pops up.
4. Click on the + next to action and select actionPerformed. Click Next >.
5. Select Method Call, then select go() from the list. Click Finish.

Run the application again. When you click on the second button, the BumperSticker component animates the color of the heart.

Again, notice how you have produced a functioning application without writing any code.

JavaBeans, Oracle Java Certifications, Oracle Java Guides, Oracle Java Learning

«« Previous
Next »»