Java Reference
In-Depth Information
[filename="src/main/java/gui/JFileFilter.java", language="java", identifier="main"]
snippet~~~~
ERROR: Failed to load src/main/java/gui/JFileFilter.java
snippet~~~~
Choosing a Color
Problem
You want to allow the user to select a color from all the colors available on your computer.
Solution
Use Swing's
JColorChooser
.
Discussion
OK, so it may be just glitz or a passing fad, but with today's displays, the 13 original AWT
colors are too limiting. Swing's
JColorChooser
lets you choose from zillions of colors.
From a program's view, it can be used in three ways:
▪ Construct it and place it in a panel
▪ Call its
createDialog()
and get a
JDialog
back
▪ Call its
showDialog()
and get back the chosen color
We use the last method because it's the simplest and the most likely to be used in a real ap-
plication. The user has several methods of operating the chooser, too:
Swatches mode
The user can pick from one of a few hundred color variants.
HSB mode
This one's my favorite. The user picks one of Hue, Saturation, or Brightness, a standard
way of representing color value. The user can adjust each value by slider. There is a huge
range of different pixel values to choose from, by clicking (or, more fun,
dragging
) in the
central area. See
Figure 14-11
.