Skip to Main Content
Java Swing, 2nd Edition
book

Java Swing, 2nd Edition

by Dave Wood, Robert Eckstein, Marc Loy, James Elliott, Brian Cole
November 2002
Intermediate to advanced content levelIntermediate to advanced
1278 pages
38h 26m
English
O'Reilly Media, Inc.
Content preview from Java Swing, 2nd Edition

The Color Chooser

As the name indicates, the JColorChooser component is designed to allow users to pick a color. If your application supports customized environments (like the foreground, background, and highlight colors for text), this control might come in handy. You can pick a color from a palette and then look at that color in a preview panel that shows you how your color looks with black and white. The dialog also has an RGB mode that allows you to pick the exact amounts of red, blue, and green using sliders. The standard color chooser window looks like Figure 12-7.

The default JColorChooser dialog in Swatches (left) and RGB (right) modes

Figure 12-7. The default JColorChooser dialog in Swatches (left) and RGB (right) modes

The JColorChooser class provides a static method for getting this pop up going quickly. Here’s the code that produced the screens in Figure 12-7:

// ColorPicker.java // A quick test of the JColorChooser dialog // import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ColorPicker extends JFrame { public ColorPicker( ) { super("JColorChooser Test Frame"); setSize(200, 100); final Container contentPane = getContentPane( ); final JButton go = new JButton("Show JColorChooser"); go.addActionListener(new ActionListener( ) { public void actionPerformed(ActionEvent e) { Color c; c = JColorChooser.showDialog(((Component)e.getSource( )).getParent( ), "Demo", Color.blue); contentPane.setBackground(c); } }); contentPane.add(go, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java Threads, 3rd Edition

Java Threads, 3rd Edition

Scott Oaks, Henry Wong

Publisher Resources

ISBN: 0596004087Errata PageSupplemental Content