Java Book
Java Book
Web development
May . 2006
magazine
NetBeans Profiler
Matisse in Action
Plug-in Showcase
Bringing
Light
to
Java
Development
Exploring GroupLayout
Learn details about the layout
manager that powers Matisse
magazine
Assistant Editor
Osvaldo Pinali Doederlein
[email protected]
Number One
Graphic Designers
Tarcsio Bannwart, Jaime Peters Jr,
Tersis Zonato, Lais Pancote
creation. Beginners will learn how to develop a complete desktop application using
Illustrators
Felipe Machado & Francisco Peixoto
profiling tools, and further learn about GroupLayout, the layout manager that is the
Matisse and other IDE facilities. Seasoned programmers will also benefit, knowing
details about the NetBeans Profiler, which introduces breakthrough innovations in
core of Matisse. Also shown is how to use IDE features and modules to detect bug
patterns, enforce code conventions, and closely follow coding rules that promote
Contributors
Fernando Lozano
Geertjan Wielenga
Gregg Sporar
Leonardo Galvo
Osvaldo Doederlein
Tomas Pavek
Editorial Support
Robert Demmer
John Jullion-Ceccarelli
NetBeans Magazine is
supported by NetBeans.org
Java and all Java-based marks are trademarks or registered
trademarks of Sun Microsystems, Inc. in the United States and
other countries. NetBeans Magazine is independent of Sun
Microsystems, Inc.
Although every precaution has been taken in the preparation of
this magazine, the publisher assumes no responsibility for errors
or omissions, or for damages resulting from the use of
the information herein contained.
Produced in Brazil
increase the rhythm, with version 5.5 at the door and the first releases of 6.0 already
available. The best part is you dont get only to watch. You can join in, and participate
in this movement thats bringing light to Java development.
Happy coding,
Leonardo Galvo
Contents
19
22
32
Exploring the
NetBeans Profiler
An in-depth exploration
and tutorial about the best
profiling tool on the market
Plug-in Module
Showcase
Enrich your development
experience with dozens of
NetBeans extensions
Web Development
with NetBeans 5
Use the best of NetBeans to
develop and debug JSPs, Servlets,
and Struts applications
Get to Know
GroupLayout
Learn details about the new
layout manager that powers the
Matisse GUI builder c
04
A Complete App
Using NetBeans 5
44
58
May . 2006
magazine
NetBeans Profiler
Matisse in Action
Plug-in Showcase
Bringing
Plug-in Module
Quick Start
52
Light
to
Java
Development
Exploring GroupLayout
Learn details about the layout
manager that powers Matisse
First Edition N
A Complete
App
Using NetBeans 5
in
s
n
a
tBe the
e
N
n
g
Lear ice usin ilder
u
t
Prac se GUI B
s
Mati o Lozano
and
Fern
etBeans is not a
The reader will need basic Swing and JDBC skills, beyond familiarity
including a quick tour of the IDE user interface. Then the sample
IDE features.
The first part of this article will be more detailed, because the
As we move deeper into the application logic, the article will switch
to a higher level discussion. That way, this article aims for two
objectives:
features.
The first step prototypes the UI design, where NetBeans really shines.
The second step focuses on user interaction and event handling; its
actually a second prototype for the application. The third and last
with the MVC architecture will note these steps form a process that
installer.
starts with the View, then builds the Controller, and finally builds the
Model.
Installing NetBeans
Just visit netbeans.org and click on NetBeans IDE 5.0 under the
can choose installers for your platform, including Windows, Mac OS,
NetBeans
IDE home
page
How-
like the javac compiler, so a JRE wont be enough. If you dont yet
have a JDK, there are download options bundling the latest JDK with
G
netbeans.org
the IDE.
sources and extras after the download form, and download instead
the NetBeans IDE 5.0 Archive, choosing the .zip format. After all,
First Edition N
IDE. Windows users will use the netbeans.exe file, while Linux users
will use the netbeans file. The IDE will open with a welcome page (see
one displayed.
Figure 1).
E If you have used the archive instead of the native installer, youll get a license
agreement dialog on the IDEs first run. Dont worry; the Sun Public License (SPL)
used by NetBeans is an OSI-approved open source software license.
At the top of the IDE window, you see the main menu and toolbar.
If you dont like the big toolbar icons configured by default, righ-click
any empty spot in the toolbar and choose the Small Toolbar icons
menu entry.
A1
Figure 1
The NetBeans
main window
and welcome
page
A
Figure 2
NetBeans Library
Manager showing
default libraries
configured with
NetBeans 5.0
A2
N NetBeans Magazine
A3
A
Figure 3
NetBeans Options
window: changing
the default codegeneration style
for event listeners
A4
A
Figure 4
NetBeans
Advanced Options
window, enabling
text anti-aliasing
for code editors.
persist data.
and you can go very far developing the code to show and change
more readable.
Developing
the sample app
A5
A
Figure 5
A sketch for the
Todo user interface
First Edition N
netbeans.org/catalogue
these objects without the need of persistent storage. Only when you
productivity, and mixes well with TDD and other Extreme Programming
practices, while keeping costs and schedule under control and meeting
three steps:
todo.Main). Java conventions dictate that you should use your company
DNS name as a prefix for all your package names, but in this example
Now right-click the todo package icon and choose New JFrame
A
Figure 6
Visual editor with
an empty JFrame
A6
N NetBeans Magazine
A7
A
Figure 7
Visual guidelines
help positioning
and sizing controls
in the visual editor
this process.
Repeat the process to insert a JLabel attached to the left, bottom
and right corners of the JFrame. This label will be used as a status
message area for the tasks window. Then insert a JScrollPane,
attaching it to the left and right corners of the JFrame and to the
in Java SE 6.0. (You can learn more about
bottom of the JToolbar and top of the JLabel. Just leave some spacing
between the JScrollPane and JFrame borders, the JToolbar and the
Now try resizing the JFrame content panel (the drawing area). The
visual design.
Select the Toolbar icon
on the palette
By now you should have a pretty good idea about how to use
the JToolbar and a JTable to the JScrollPane, the TasksWindow class will
as shown in Figure 7.
the JTable.
the
container.
A8
constraints
to
A9
A
Figure 8
Positioning and resizing
the toolbar so it is attached
to the left, top and right
corners of the JFrame.
Notice the guidelines over
the drawing borders
A
Figure 9
Positioning the
JLabel and the
JScrollPane
First Edition N
A
Figure 11
JTable model
customizer after
adding a few
columns
A 11
A
Figure 12
TasksWindow UI
so far
A 12
icon for the component. After selecting the Classpath radio button,
A
Figure 10
NetBeans
customizer
for the icon
property Library
Manager, showing
default libraries
configured with
NetBeans 5.0
the Select File button will allow you to browse the icon files and select
interactively the one to use (including icon files inside jar packages).
see Figure 11. Well use it to make the JTable appear in the visual
editor with correct column names and some sample data. This is
customization
A 10
of
development
custom
Java
cell
and
models
because JTables
classes
renderers
column
10 N NetBeans Magazine
like
and Help.
Designing the
task details dialog
JCheckBoxMenuItem,
the Java GUI Forms category. We start with a JLabel attached to the
JMenuItem,
left, top and right borders of the dialog, with no spacing. It will serve
as a message area for validation errors and the like. Set its opaque
property and the background color so it looks like a band at the top
Now add three JLabels for the description, priority and due date
fields. Attach all three to the left of the JDialog internal area (the
TasksWindow.
and the border. Resize the two shorter labels to attach their right
A 13
A
Figure 13
Adding items do
a JMenu inside a
JMenuBar
Figure 16.
The JSpinner does not provide any property
to set a preferred or minimum width, while
the JTextField and JFormattedTextField use
the column property for this. But you can
resize the JSpinner and Matisse will set the
components preferred size in pixels.
A 14
A
Figure 14
Complete
menus for the
TasksWindow
JFrame
First Edition N 11
A 15
Figure 15
Sizing the
TaskDetailsDialog
labels
problems
positioning
in
TaskDetailsDialog. Figure
A 16
statements.
You can then use command Run | Run Main
the
17
button
Figure 16
The free design
layout aligns
different kinds of
controls by the
text baseline
(actually you can select any control you want) and right-click
any of the selected buttons. Then check the Same Size | Same
Width checkbox menu item. The drawing area will indicate that
Todo.jar archive.
A 17
Figure 17
Finished
prototype for the
TaskDetailsDialog
Note
the
library
swing-layout-1.0.jar
End of Step 1
This prototype is almost the finished
application from the UI design perspective,
but in real projects you shouldnt spend too
much time perfecting its looks. Remember,
A
Figure 18
The IDEs visual
hint for same-size
components
12 N NetBeans Magazine
A 18
A 19
Figure 19
Distributable files
for deploying
the prototype to
end-users
todo.Main (todo/Main.java)
(...)
public static void main(String[] args) {
JFrame w = new TasksWindow();
w.pack();
w.setVisible(true);
}
todo.view.TasksWindow (todo/view/TasksWindow.java)
(...)
private void addButtonActionPerformed (
java.awt.event.ActionEvent evt) {
JDialog d = new TaskDetailsDialog(this, true);
d.pack();
d.setVisible(true);
}
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 1.5.0_05-b05 (Sun Microsystems Inc.)
Main-Class: todo.Main
Class-Path: lib/swing-layout-1.0.jar
X-COMMENT: Main-Class will be added automatically by build
tion
2. Right-click the Todo project icon on the NetBeans Project window and select the Properties
menu item.
3. Select the Libraries category and click
the button Add JAR/Folder, and browse for
the archive napkinlaf.jar inside the folder
G
napkinlaf.sf.net
Napkin
custom
Swing LAF
home page
Dswing.defaultlaf=net.sourceforge.napkinlaf.Nap-
A S1
A
Figure S1
Sketch for the
Todo user interface
Rebuild
the
application
so
the
First Edition N 13
Todo Applicaton
Architecture
The second step building
the
dynamic
netbeans.org/community/releases/55/
NetBeans
5.5 preview,
featuring
UML CASE
features
A
Figure 20
UML class diagram
for the main
application classes
14 N NetBeans Magazine
prototype
aims to implement as
a persistent storage or
mock objects.
implementing complex
Task
for
moving
information
between
build this second prototype is most users have trouble imagining how
an application should behave if theres nothing they can click and see
results in.
and data access. Such objects should have the same public interface
as the real objects (and will help validate the intended interface before
its implemented and changes become too expensive), but will return
hard-coded data. You can use the Java collections API so the user can
A 20
article.
Now
well
create
Java
packages
todo.controller
and
todo.model.
G
argouml.org
ArgoUML,
Open Source
UML CASE
tool written
in Java
Item 4 can be tested with a mock object that simply adds or removes
Customizing a JTable
the task list (which corresponds to Task attributes) and change each
row background colors according to the task status: red for late
tasks, yellow for tasks with an alert set, blue for completed tasks,
and white otherwise.
A 21
Most task list columns have a short content and can have fixed
width. Just the description column/attribute can have wider content,
so it should get whatever space is left after the fixed-width columns
are sized. To implement all these features, we need to create three
A
Figure 21
All classes for the
Todo application
classes:
The TasksTableModel class receives requests for data at a specific
row/column from the JTable control and returns a task attribute
value, such as the description or due date, for the task at the given
row. It also has the ability to filter the task collection to exclude
completed tasks, or to change the sorting criteria. Sometimes it
will be better to leave these sorting and filtering tasks to the model
(which can delegate them to the database) but if the dataset is
not too big, doing these operations in-memory will improve user
experience.
The TaskColumnModel class adds columns to the JTable and
configures each column with its preferred width, label and
First Edition N 15
java.sun.com/docs/books/tutorial/uiswing
categories:
are not exposed as part of the view classes public interfaces. For
example, the selection of a task should enable the Edit task and
editor.
ESwing itself was designed using the MVC architecture, but dont be
A 22
todo.view.ActionSupport
class.
This
A
Figure 23
Event handler
declarations are
guarded (i.e. noneditable) code
sections in the
source editor
16 N NetBeans Magazine
A 23
and
so
on.
Most of the work in this step was just coding. NetBeans provides
nice code editors and a good debugger that eases the task
and refactoring support. But it can go beyond: its very easy to build
and so on.
Model classes
The TaskManager class is a DAO (Data Access Object). Being the
only DAO on the application, it contains many methods that would
otherwise be in an abstract superclass. Its implementation is very
simple, so theres lots of room for improvement.
database if there isnt one available. But its flexible enough to allow
the user to open task databases at other locations, and remember
End of Step 2
storage.
G
hsqldb.org
HSQLDB, an
Open Source
embedded
100%-Java
database
usually need a way to tap into the database. Maybe they need to
A 24
Figure 24
Configuring the
HSQLDB JDBC
driver in the IDE
First Edition N 17
Fernando Lozano
([email protected])
is an independent consultant with more than
10 years experience doing IS development and
network integration. He
has many professional
certifications, and is also
a technical writer and
open source software
advocate, serving as
Linux.java.net community manager, LPI Brazil
Technical Director and
GNU Project webmaster
in charge of Brazillian
Portuguese translations.
A
Figure 25
Connecting to
the Todo task
database
Now right-click the HSQLDB driver icon, and choose the Connect
using menu item. Provide the parameters to connect to your local
under Windows.
Then you can open the connection and browse the database
catalog for tables, indexes and other database objects. Each item
has a context menu for operations like creating new tables, altering
columns and viewing data (Figure 26). Most operations
A 25
Figure 26
Executing SQL
statements
shown
features
that
were
many
NetBeans
18 N NetBeans Magazine
NetBeans
goes
A 26
netbeans.org/community/
magazine/code/nb-completeapp.zip