JColorChooser – Java Swing – Example
In this tutorial, we are going to see an example of JColorChooser in Java Swing. JColorChooser class is used to create a dialog box that allows color selection so that the user can select any color. It inherits from JComponent class.

JColorChooser constructors class:
| JColorChooser() | It is used to create a color selection panel with a white color initially. |
| JColorChooser(color init) | It is used to create a color selection panel with the initially specified color. |
Commonly used methods:
- addChooserPanel(AbstractColorChooserPanel panel) : It is used to add a color selection panel to the color picker.
- Color showDialog(Component c, String title, Color initialColor) : It is used to display the color picker dialog box.
Example of JColorChooser in Java Swing:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class ChooseColor extends JFrame implements ActionListener
{
Container container;
ChooseColor()
{
container = getContentPane();
container.setLayout(new FlowLayout());
JButton btn = new JButton("Color");
btn.addActionListener(this);
container.add(btn);
}
public void actionPerformed(ActionEvent e)
{
Color init = Color.BLUE;
Color coleur = JColorChooser.showDialog(this,"Choose a color",init);
container.setBackground(coleur);
}
public static void main(String[] args)
{
ChooseColor frame = new ChooseColor();
frame.setSize(300,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Output:





