19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

dialog is set modal in line 24 in<br />

ColorDialog.java (Listing 38.6). If the dialog<br />

is not modal, all the statements in the Change<br />

But<strong>to</strong>n Text Color but<strong>to</strong>n handler are executed<br />

before the color is selected from the dialog<br />

box.<br />

38.8 JColorChooser<br />

You created a color dialog in the preceding example as a subclass<br />

of JDialog, which is a subclass of java.awt.Dialog (a <strong>to</strong>p-level<br />

heavy-weight <strong>com</strong>ponent). Therefore, it cannot be added <strong>to</strong> a<br />

container as a <strong>com</strong>ponent. Color dialogs are <strong>com</strong>monly used in GUI<br />

programming. Swing provides a convenient and versatile color<br />

dialog named javax.swing.JColorChooser. JColorChooser is a<br />

lightweight <strong>com</strong>ponent inherited from JComponent. It can be added<br />

<strong>to</strong> any container. For example, the following code places a<br />

JColorChooser in an applet, as shown in Figure 38.18.<br />

<br />

public class JColorChooserDemo extends javax.swing.JApplet {<br />

public JColorChooserDemo() {<br />

this.add(new javax.swing.JColorChooser());<br />

}<br />

}<br />

Figure 38.18<br />

An instance of JColorChooser is displayed in an applet; (b)<br />

Often an instance of JColorChooser is displayed in a dialog box<br />

using JColorChooser's static showDialog method:<br />

public static Color showDialog(Component parentComponent,<br />

String title,<br />

Color initialColor)<br />

For example, the following code displays a JColorChooser, as<br />

shown in Figure 38.18.<br />

Color color = JColorChooser.showDialog(this, "Choose a color",<br />

Color.YELLOW);<br />

29

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!