Java Reference
In-Depth Information
invoke this method to display the color-chooser dialog. Method showDialog returns the
selected Color object, or null if the user presses Cancel or closes the dialog without
pressing OK . The method takes three arguments—a reference to its parent Component , a
String to display in the title bar of the dialog and the initial selected Color for the dialog.
The parent component is a reference to the window from which the dialog is displayed (in
this case the JFrame , with the reference name frame ). The dialog will be centered on the
parent. If the parent is null , the dialog is centered on the screen. While the color-chooser
dialog is on the screen, the user cannot interact with the parent component until the dialog
is dismissed. This type of dialog is called a modal dialog.
After the user selects a color, lines 40-41 determine whether color is null , and, if so,
set color to Color.LIGHT_GRAY . Line 44 invokes method setBackground to change the
background color of the JPanel . Method setBackground is one of the many Component
methods that can be used on most GUI components. The user can continue to use the
Change Color button to change the background color of the application. Figure 13.8 con-
tains method main , which executes the program.
Figure 13.8(b) shows the default JColorChooser dialog that allows the user to select a
color from a variety of color swatches . There are three tabs across the top of the dialog—
Swatches , HSB and RGB . These represent three different ways to select a color. The HSB tab
allows you to select a color based on hue , saturation and brightness —values that are used to
define the amount of light in a color. Visit http://en.wikipedia.org/wiki/HSL_and_HSV
for more information on HSB. The RGB tab allows you to select a color by using sliders to
select the red, green and blue components. The HSB and RGB tabs are shown in Fig. 13.9.
Fig. 13.9 |HSB and RGB tabs of the JColorChooser dialog. (Part 1 of 2.)
 
Search WWH ::




Custom Search