Demonstrates the use of a JColorChooser which presents a dialog with three different tabbed panes to allow the user to select a color preference. The dialog returns a Color object based on the user's selection or null if the user entered Cancel.
import java.awt.*; import java.awt.event.*; import javax.swing.*; /** Simple example illustrating the use of JColorChooser. * * . */ public class JColorChooserTest extends JFrame implements ActionListener { public static void main(String[] args) { new JColorChooserTest(); } public JColorChooserTest() { super("Using JColorChooser"); WindowUtilities.setNativeLookAndFeel(); addWindowListener(new ExitListener()); Container content = getContentPane(); content.setBackground(Color.white); content.setLayout(new FlowLayout()); JButton colorButton = new JButton("Choose Background Color"); colorButton.addActionListener(this); content.add(colorButton); setSize(300, 100); setVisible(true); } public void actionPerformed(ActionEvent e) { // Args are parent component, title, initial color. Color bgColor = JColorChooser.showDialog(this, "Choose Background Color", getBackground()); if (bgColor != null) getContentPane().setBackground(bgColor); } }