Java Reference
In-Depth Information
How It Works
The
Action
object that you create for custom color operations takes care of virtually everything you
need. Custom color choosing is dealt with in the
actionPerformed()
method for the
Action
object that
you use to create custom color menu items and the toolbar button. Using the
ImageIcon
class as the basis
for the menu item and toolbar button icons is the key to being able to customize these for custom colors.
You could have created all the icons used in Sketcher programmatically. If you really wanted to go to
town, you could draw the icons for the element types in the current color!
The
JColorChooser
class defines a complete color-choosing facility that you can use in your own dialog,
or you can create a complete modal dialog by calling the
static
method
showDialog()
as you have
done here. The arguments to
showDialog()
are a reference to the parent component for the dialog, the
title for the dialog, and the initial color selection. You can choose a color using any of the three tabs,
which provide different mechanisms for defining the color that you want. When you click OK, the color
that you chose is returned as type
Color
. If you exit the dialog by any means other than selecting the OK
button, the dialog returns
null
. You just store the color that is returned in
elementColor
and set it in the
status bar pane. Subsequent sketching operations are in the custom color.
SUMMARY
In this chapter you learned how to use dialogs to manage data input. You have also learned how to implement
context menus, which can bring a professional feel to the GUI in your applications. You have applied scroll-