Java Reference
In-Depth Information
A simpler alternative to creating a dialog is to use another static
JColorChooser
method to display a
standard customizable dialog:
Color color = JColorChooser.showDialog(this, "Select Custom Color",
customColor);
This statement displays a dialog with the current window as the parent and with the default color as
cus-
tomColor
. If the OK dialog button is clicked, the color that was selected is returned. If the dialog is closed
without choosing a color, by clicking the Cancel button for example,
null
is returned. We use this approach
in Sketcher.
Adding the Custom Color GUI
You add a menu item to the
Color
menu, a toolbar button to the toolbar, and a menu item to the general
pop-up menu. First, you need to add an additional
Action
member to the
SketcherFrame
class:
private ColorAction redAction, yellowAction,greenAction,
blueAction, customAction;
The
Action
objects for colors are set up in the
createElementColorActions()
method in
Sketcher-
Frame
:
private void createElementColorActions() {
// Create color Action objects as before...
customAction = new ColorAction("Custom...", BLUE, 'C',
CTRL_DOWN_MASK|ALT_DOWN_MASK);
// Initialize the array
ColorAction[] actions = {redAction, greenAction, blueAction,
yellowAction, customAction};
colorActions = actions;
// Add toolbar icons as before...
customAction.putValue(LARGE_ICON_KEY, CUSTOM24);
// Add menu item icons as before...
customAction.putValue(SMALL_ICON, CUSTOM16);
// Add tooltip text to actions as before...
customAction.putValue(SHORT_DESCRIPTION, "Draw in custom color");
}
Directory "Sketcher 11 with custom colors"
You set up the new
Action
object for custom color GUI components and equip it with large and small
icons and the text for a tooltip.