Get started with a JColorChooser

Instantiate a JColorChooser component and add it to a container. To receive an event when the selected color has changed, add a ChangeListener to the selection model of the colorchooser and implement the method stateChanged.

This example shows you how to listen for color change events and modify the background color of a JPanel.

Main.java:

import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
 
public class Main extends JFrame {
   public Main() {
      super("JColorChooser Demonstration");
 
      getContentPane().setLayout(new GridLayout(1, 2)); 
      final JPanel panel = new JPanel();
      final JColorChooser colorChooser = new JColorChooser();
 
      getContentPane().add(panel);
      getContentPane().add(colorChooser);
 
      colorChooser.getSelectionModel().addChangeListener(
         new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
               Color newColor = colorChooser.getColor();
               panel.setBackground(newColor);
            }
         }    
      );
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
   }
 
   public static void main(String[] args) {
      Main main = new Main();
      main.pack();
      main.setVisible(true);
   }
}