Java Reference
In-Depth Information
A text area component, like a text field, can be set so that it is either edit-
able or noneditable. The user can change the contents of an editable text area
by clicking on the text area and typing with the keyboard. If the text area is
noneditable, it is used to display text only. By default, a JTextArea component
is editable.
A JFileChooser component makes it easy to allow users to specify a specific
file to use. Another specialized dialog box—one that allows the user to choose a
color—is discussed in the next section.
10.9 Color Choosers
In many situations we may want to give the user of a pro-
gram the ability to choose a color. We could accomplish
this in various ways. For instance, we could provide a list of
colors using a set of radio buttons. However, with the wide
variety of colors available, it's nice to have an easier and
more flexible technique to accomplish this common task. A
specialized dialog box, often referred to as a color chooser ,
is a graphical component that serves this purpose.
The JColorChooser class represents a color chooser. It can be used to display
a dialog box that lets the user click on a color of choice from a palette presented
for that purpose. The user could also specify a color using RGB values or other
color representation techniques.
The program shown in Listing 10.14 uses a color chooser dialog box to specify
the color of a panel that is displayed in a separate frame.
After a color has been chosen. the new color is displayed in the primary frame
and another dialog box (this one was created using JOptionPane as discussed in
Chapter 6) is used to determine if the user wants to change the color again. If so,
another color chooser dialog box is displayed. This cycle can continue as long as
the user desires.
Invoking the static showDialog method of the JColorChooser class causes
the color chooser dialog box to appear. The parameters to that method
specify the parent component for the dialog box, the title that appears in the
dialog box frame, and the initial color showing in the color chooser. By using
the variable shade as the third parameter, the color initially showing in the
color chooser when it first appears will coincide with the current color of the
panel.
KEY CONCEPT
A color chooser allows the user to
select a color from a palette or use
RGB values.
 
Search WWH ::




Custom Search