Java Reference
In-Depth Information
containing the icons for the Sketcher application in a directory with the path
D:\Sketcher\Images
and spe-
cifying the full path to the location of each icon. You can put them elsewhere if you want, on your C: drive
perhaps.
The
ImageIcon
constructors accept icon files in PNG format (Portable Network Graphics format, which
have the extension
.png
), GIF format (Graphics Interchange Format, or
.gif
files), or JPEG format (Joint
Photographic Experts Group format, or
.jpg
files) formats. I'm assuming GIF files in the Sketcher code. If
you want to use files in one of the other formats, just modify the code for the example accordingly.
A large icon in an
Action
object applies to a toolbar button. This is identified by the
LARGE_ICON_KEY
key. A large icon is usually 24 ×24 pixels, whereas a small icon that might be used on a menu item is usually
16 × 16 pixels. Obviously, a large icon can have more detail in the image.
If you add a static import statement interface to
SketcherFrame.java
, for the keys that are defined in
the
Action
you don't have to qualify the action property keys in the code:
import static javax.swing.Action.*;
To create a large icon for the
openAction
object from an image in a file
Open24.gif
, you could write
the following:
openAction.putValue(
LARGE_ICON_KEY, new ImageIcon("D:/Sketcher/Images/Open24.gif"));
This creates the
ImageIcon
object from the GIF file and stores it in the
Action
object; the icon is as-
sociated with the
LARGE_ICON_KEY
key. The key identifies the icon as that to be used for a toolbar button.
If the
ImageIcon
constructor cannot find the file, the object created does not contain an image; you get no
indication of this. The icon specified by the
Open24.gif
file is 24 × 24 pixels, so is ideally suited for use
as a toolbar button. Of course, if you have already created the Icon object, you can pass the reference to the
putValue()
method.
NOTE
You can use the GIF files containing the icons for the toolbar buttons that are in the
code download for the topic or create some for yourself. You can download the code from the
You can use any graphics editor that can save files in the GIF format to create your own —
Paint Shop Pro, Microsoft Paint, or GIMP, for example. Menu item icons should be 16 × 16
pixels and toolbar button icons 24 × 24 pixels.
You can also download icons for a variety of applications from the Java look-and-feel graph-
been designed specifically to go with the Java look-and-feel. The icons are available as 16 ×
16 pixels and 24 × 24 pixels. The first four icons on the toolbar shown at the beginning of this
section are icons from this set — the others I created for myself.
Let's add some toolbar buttons to Sketcher.