We can’t have a full discussion of beans without spending a little time talking about the builder environments in which they are used. In this chapter, we use the NetBeans IDE version 7.2 to demonstrate our beans. NetBeans is a popular, pure Java development environment. In this case, the integrated in integrated development environment means that NetBeans offers powerful source and visual editor capabilities, templates that aid in the creation of various types of Java classes, and the ability to compile, run, and debug applications, all in one tool. In particular, we’ll be using the visual development environment of NetBeans to glue together our Java beans and try them out. Other IDEs offer similar capabilities. See Appendix A for a brief comparison of NetBeans and the Eclipse IDE. We’ve chosen NetBeans to use in this chapter because its mature GUI builder environment makes it easy to illustrate the concepts here.
Because NetBeans is a full-blown production development environment, it has many features we don’t use in these examples. For that reason, we can’t really provide a full introduction to it here. We will provide only bare-bones directions here for demonstrating the Java beans in this chapter. Most of the techniques you see here will apply in some form in other IDEs as well.
You should install Java 7.0 before you install NetBeans. If you installed the JDK from Sun’s website, you may have downloaded a combined JDK and NetBeans bundle, so you may already have it installed. The standalone installation of NetBeans can be found at http://www.netbeans.org. Follow the simple installation instructions for those packages (you may have to reboot if you just installed Java). When you’re all set, launch NetBeans. When you first start NetBeans, a welcome screen appears. Close it for now by clicking the small x on its tab. (If you want to return to this welcome screen later to take the NetBeans tutorials, choose Help → Welcome Screen.)
Before we get started, we’ll have to add the example beans used in this chapter to the NetBeans palette. NetBeans installs Java beans in a central location that can be used by multiple projects in the editor.
To install our beans, grab our demonstration beans JAR file, magicbeans.jar, from this book’s website.
Save the file locally and then select Tools → Palette → Swing/AWT Components to add the beans.
Click “Add from Jar,” then navigate to and select the magicbeans.jar file and click Next. The wizard then shows you a list of beans that our JAR contains (there should be eight altogether).
Select all of them (click the first and shift-click the last), then click Next.
NetBeans prompts you for the palette category under which you wish to file these; select Beans (which would otherwise be empty), then click Finish and Close.
We’ll see these beans soon when we start editing an application.
Now, we must create a new project to hold our work. To create a new project, select New Project from the File menu. In the Categories pane of the wizard, select General and in the Projects pane, select Java Application, and then click Next. Give your project a name, such as LearningJava, and specify a location or leave the defaults. Uncheck the box labeled Create Main Class because we will create our own, then click Finish to create our empty project. If this is the first time you’ve created a project in NetBeans, you may see a message that says that NetBeans is scanning project classpaths; this may take a few minutes.
Next, we need to create a class file to put into our project.
Select New File from the File menu. NetBeans prompts you with a
wizard. In the Categories pane, select Java GUI Forms and in the File
Types pane, select JFrame Form. Choosing JFrame Form gives us a Java
class file extending
the basic structure of a GUI application already set up for us. Click
Next, then give the file a name, such as LearningJava1. You may leave
the package set to the default package if you wish. Now click Finish;
the screen looks like Figure 22-1.
Figure 22-1 shows the NetBeans application. The screen has three main areas with a toolbar at the top. The left area is the explorer window. Tabs in the explorer window can show a Java package and class-oriented view of our project, a filesystem view, or a runtime environment view. The explorer is shown with our LearningJava1.java class selected. The bottom left is the Navigator area, which shows the methods and properties of the class, and in the case of a GUI component the child component layout within it. In the center area is our workspace with tabs for each open file (currently one). Because we selected a GUI-type file, NetBeans has placed us into GUI “design” mode and shows us an empty box where we will place our beans. Click alternately on the Source and Design buttons to switch between the Java source code and this view (try it out).
On the right side are a Palette pane and a Properties pane, which is currently showing some properties of the currently selected file, but will show the properties of the currently selected bean component when we add one to the design area. The Palette has groupings for different types of beans. The Swing group includes all the standard Swing components. AWT holds older AWT components. Layouts holds Java layout managers.
Under Palette, select Swing (it may be selected by default) to
see some of the available Swing components (they appear as icons at
the top of the window). Now scroll down and select Beans to see the
beans we imported earlier. You should see the friendly
Dial component bean from Chapter 18, along with a tiny person bean. The
rest of our beans lack pretty icons because these example beans aren’t
packaged with them. (We’ll talk about packaging later in the chapter.)
Figure 22-2 shows the bean
To place a bean into the workspace, click on it and then click
in the workspace. Before you do that, though, you may want to set an
appropriate layout manager. The Inspector holds a tree that shows all
the components (visible and invisible) in the project. By
right-clicking on the
top-level container) in either the workspace or the tree, you can
select Set Layout to specify the layout manager for the frame. For
now, try using AbsoluteLayout, provided by NetBeans. This allows you
to arbitrarily place and move beans within the container. You wouldn’t
want to do this in general, but for our examples it will make life a
bit easier. The other layout managers are easy enough to use, but
we’ll refer you to NetBeans tutorials for the details.