Java Reference
In-Depth Information
A true argument to the method will allow the toolbar to float, so you can switch this on and off in your
program as you wish. You can also test whether the toolbar can float by calling the isFloatable()
method for the JToolBar object. This will return true if the toolbar is floatable, and false
otherwise. If you recompile SketchFrame and run Sketcher again you will see that the gray bit at the
left hand end of the toolbar is no longer there, and you cannot drag the toolbar around.
The button that has been created uses the name from the Action object as its label by default. We
really want toolbar buttons with icons, so that's got to be the next step.
Adding Icons
A reference to an icon is generally stored in a variable of type Icon . Icon is an interface that declares
methods to obtain the height and width of an icon in pixels - these are the getHeight() and
getWidth() methods respectively, and to paint the icon image on a component - the
paint() method. One class that implements the Icon interface is ImageIcon and it is this class that
you use to create an icon object in your program from a file containing the icon image. The class
provides several constructors that create an ImageIcon object and the one we will use accepts a
String argument that specifies the file where the icon image is to be found. The String object can be
just a file name, in which case the file should be in the current directory - the one that contains the
.class files for the application or applet. You can also supply a string that specifies the path and file
name where the file containing the image is to be found. The ImageIcon constructors accept icon files
in PNG (Portable Network Graphics format, which have .png extensions), GIF ( G raphics I nterchange
F ormat, or .gif files), or JPEG ( J oint P hotographic E xperts G roup format, .jpg files) formats, but we
will assume GIF files in our code.
We will put the icons for Sketcher in a subdirectory of the Sketcher directory called Images , so create a
subdirectory to your Sketcher application directory with this name. To create an icon for the
openAction object from an image in a file open.gif in the Images directory, we could write:
openAction.putValue(Action.SMALL _ ICON, new ImageIcon ("Images/open.gif"));
This stores the ImageIcon object in our Action object associated with the SMALL _ ICON key. The
add() method for the toolbar object will then look for the icon for the toolbar button it creates using
this key. Let's see if it works.
You will need to create the GIF files containing the icons. Any graphics editor that can save files in
the GIF format will do, Paint Shop Pro, Microsoft Paint, or gimp, for instance. I created my icons
as 16x16 pixels since it is a fairly standard size for toolbar buttons. Make sure the file for the
openAction object is called open.gif , and stored in the Images subdirectory. We will need
GIF files for other buttons too, and they will each have a file name that is the same as the label on
the corresponding menu item.
If you want to put them together in one go, for the file menu toolbar button you will need
save.gif , new.gif and print.gif , for the element types you will need line.gif ,
rectangle.gif , circle.gif , and curve.gif , and for the colors you will need
red.gif , yellow.gif , green.gif and blue.gif . GIF files for all these icons are
available along with the Sketcher source code at the Wrox web site:
http://www.wrox.com/Consumer/Store/Download.asp .
Search WWH ::




Custom Search