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.

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, BorderLayout.SOUTH);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	public static void main(String args[]) {
		ColorPicker cp = new ColorPicker();
		cp.setVisible(true);
	}
}