epubjs
Version:
Render ePub documents in the browser, across many devices
97 lines (96 loc) • 11.2 kB
HTML
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>The NetBeans IDE</title><link rel="stylesheet" href="core.css" type="text/css"/><meta name="generator" content="DocBook XSL Stylesheets V1.74.0"/></head><body><div class="sect1" title="The NetBeans IDE"><div class="titlepage"><div><div><h1 class="title"><a id="learnjava3-CHP-22-SECT-2"/>The NetBeans IDE</h1></div></div></div><p>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 <span class="emphasis"><em>integrated</em></span> in <span class="emphasis"><em>integrated
development environment</em></span> 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 <a class="xref" href="apa.html" title="Appendix A. The Eclipse IDE">Appendix A</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.</p><p>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.</p><div class="sect2" title="Installing and Running NetBeans"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-22-SECT-2.1"/>Installing and Running NetBeans</h2></div></div></div><p><a id="idx11148" class="indexterm"/> <a id="idx11161" class="indexterm"/> <a id="idx11168" class="indexterm"/>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 <a class="ulink" href="http://www.netbeans.org">http://www.netbeans.org</a>. 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
<span class="unicode">→</span> Welcome Screen.)</p><div class="sect3" title="Installing our example beans"><div class="titlepage"><div><div><h3 class="title"><a id="learnjava3-CHP-22-SECT-2.1.1"/>Installing our example beans</h3></div></div></div><p><a id="I_indexterm22_id820316" class="indexterm"/> <a id="I_indexterm22_id820326" class="indexterm"/>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.</p><div class="orderedlist"><ol class="orderedlist"><li class="listitem"><p>To install our beans, grab our demonstration beans JAR file,
<span class="emphasis"><em>magicbeans.jar</em></span>, from this book’s <a class="ulink" href="http://oreil.ly/Learn_Java_4E">website</a>.</p></li><li class="listitem"><p>Save the file locally and then select Tools <span class="unicode">→</span> Palette <span class="unicode">→</span> Swing/AWT Components to add the
beans.</p></li><li class="listitem"><p>Click “Add from Jar,” then navigate to and select the
<span class="emphasis"><em>magicbeans.jar</em></span> file and click Next. The
wizard then shows you a list of beans that our JAR contains (there
should be eight altogether).</p></li><li class="listitem"><p>Select all of them (click the first and shift-click the
last), then click Next.</p></li><li class="listitem"><p>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.</p></li></ol></div><p>We’ll see these beans soon when we start editing an
application.</p></div><div class="sect3" title="Creating a project and file"><div class="titlepage"><div><div><h3 class="title"><a id="learnjava3-CHP-22-SECT-2.1.2"/>Creating a project and file</h3></div></div></div><p><a id="idx11160" class="indexterm"/> <a id="idx11167" class="indexterm"/>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.</p><p>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 <code class="literal">JFrame</code> with
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 <a class="xref" href="ch22s02.html#learnjava3-CHP-22-FIG-1" title="Figure 22-1. The NetBeans workspace">Figure 22-1</a>.<a id="I_indexterm22_id820468" class="indexterm"/><a id="I_indexterm22_id820475" class="indexterm"/></p></div><div class="sect3" title="The NetBeans workspace"><div class="titlepage"><div><div><h3 class="title"><a id="learnjava3-CHP-22-SECT-2.1.3"/>The NetBeans workspace</h3></div></div></div><p><a id="idx11162" class="indexterm"/> <a id="idx11169" class="indexterm"/> <a class="xref" href="ch22s02.html#learnjava3-CHP-22-FIG-1" title="Figure 22-1. The NetBeans workspace">Figure 22-1</a> 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 <span class="emphasis"><em>LearningJava1.java</em></span> 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).</p><p>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.</p><div class="figure"><a id="learnjava3-CHP-22-FIG-1"/><div class="figure-contents"><div class="mediaobject"><a id="I_22_tt1212"/><img src="httpatomoreillycomsourceoreillyimages1707705.png" alt="The NetBeans workspace"/></div></div><p class="title">Figure 22-1. The NetBeans workspace</p></div><p>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 <code class="literal">Dial</code> component bean from <a class="xref" href="ch18.html" title="Chapter 18. More Swing Components">Chapter 18</a>, 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.)
<a class="xref" href="ch22s02.html#learnjava3-CHP-22-FIG-2" title="Figure 22-2. The bean palette">Figure 22-2</a> shows the bean
palette.</p><p>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 <code class="literal">JFrame</code> (our
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.<a id="I_indexterm22_id820604" class="indexterm"/><a id="I_indexterm22_id820611" class="indexterm"/><a id="I_indexterm22_id820618" class="indexterm"/><a id="I_indexterm22_id820625" class="indexterm"/><a id="I_indexterm22_id820632" class="indexterm"/></p><div class="figure"><a id="learnjava3-CHP-22-FIG-2"/><div class="figure-contents"><div class="mediaobject"><a id="I_22_tt1213"/><img src="httpatomoreillycomsourceoreillyimages1707706.png" alt="The bean palette"/></div></div><p class="title">Figure 22-2. The bean palette</p></div></div></div></div></body></html>