O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 and Feel (L&F)—either
a standard one provided by the VM or a custom one. L&Fs are a whole lot
of work to build and still aren’t completely custom. You can redefine a but-
ton to look like red stoplights, but then all buttons throughout your applica-
tion will look like red stoplights. Sometimes all you really want is a look
built entirely out of images, much like image-based web navigation.
2
|
Chapter 1, Basic JComponents
#1 Create Image-Themed Components
HACK
To give you an idea of where this hack is going, Figure 1-1 shows our target:
a frame with a panel containing a label, a button, and a checkbox. The
panel, label, and button will be completely drawn with images, using none
of the standard L&F. The checkbox will be a standard checkbox, but it
should be transparent to fit in with the image background.
The first step toward image nirvana is the background. Because this type of
component is quite reusable, I built a subclass of
JPanel called ImagePanel,
shown in Example 1-1.
The constructor takes the image to draw and saves it for later use in the
img
variable. Then it calls setSize( ) and setPreferredSize( ) with the size of the
image. This ensures that the panel will be the size of the image exactly. I had
Figure 1-1. A component rendered with images
Example 1-1. A Custom subclass of JPanel
public class ImagePanel extends JPanel {
private Image img;
public ImagePanel(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null),
img.getHeight(null));
setSize(size);
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setLayout(null);
}
}
Create Image-Themed Components #1
Chapter 1, Basic JComponents
|
3
HACK
to set the preferred, maximum, and minimum sizes as well—this is because
the panel’s parent and children may not be using absolute layouts.
Absolute layout means that there is no layout manager to
position the components appropriately (which can be set by
calling
setLayout(null)).
In this case, the explicit size and position will be used (via
setSize( )
and
setLocation( )
). When a layout manager is set, the preferred, minimum, and
maximum sizes may be used. To cover all of the bases, simply set all four
values to the image size.
Now that the panel is sized appropriately, you can paint the image by over-
riding
paintComponent( ):
public void paintComponent(Graphics g) {
g.drawImage(img,0,0,null);
}
It’s important to override paintComponent( ) instead of
paint( ), or else the child components won’t get drawn.
To test it, Example 1-2 uses an ImagePanel and the usual JFrame.
When run, the
ImageTest program looks like Figure 1-2.
Now that the background is done, it’s time to focus on the label, Activate
Reactor. This is just a static image that sits at a certain position on the back-
ground. You could use another
ImagePanel, but since the Activate Reactor
text is logically a
JLabel, you can just create an ImageLabel subclass, as
shown in Example 1-3.
Example 1-2. Testing out image-based panels
public class ImageTest {
public static void main(String[] args) {
ImagePanel panel = new ImagePanel(new
ImageIcon("images/background.png").getImage( ));
JFrame frame = new JFrame("Hack #1: Create Image-Themed Components");
frame.getContentPane( ).add(panel);
frame.pack( );
frame.setVisible(true);
}
}

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required