
1
Chapter 1
C H A P T E R O N E
Basic JComponents
Hacks 1–12
Swing is a powerful toolkit, filled to the brim with complicated compo-
nents, extension APIs, and large Model-View-Controller (MVC) systems. It
can be quite daunting. The current edition of O’Reilly’s Java Swing book
now stretches over 1,200 pages! Swing now extends from the simplest
JButton to the full Look and Feel API. I am still amazed at the power and
flexibility of Swing, and quite aware of its complexity. Some of the more
esoteric parts can take years to master. However, you don’t need to go
straight into the
JTree or Look and Feel APIs just to do something cool.
There are still a lot of fun things waiting in the standard components we
don’t always think about.
This chapter covers some of the basic components that every Swing devel-
oper uses: buttons, labels, menus, and the occasional scroll pane. From this
base you will learn how to create image buttons, put watermarks into your
text areas, and even build a new component or two. These are the compo-
nents that seem boring, but with a little imagination, they can do a whole
lot, and the techniques here lay the foundation for even more exciting hacks
later in the book.
H A C K
#1
Create Image-Themed Components Hack #1
This hack shows how to use Swing’s built-in image support to create a
completely custom image-based user interface.
Most Swing applications get their look from a Look