JColorChooser Example Java Swing

Swing JColorChooser gets your own color palette. The color you selected either with RGB or HSB style can be applied to any Swing component. In the program,it is applied to Container (indirectly to JFrame).
Example on JColorChooser
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JColorChooserDemo extends JFrame implements ActionListener
  JButton btn;
  Color clr;
  Container c;
  public JColorChooserDemo()
    c = getContentPane() ;
    c.setLayout(new FlowLayout());
    btn = new JButton("Change Color");

    clr = JColorChooser.showDialog(this, "Choose Your Color", Color.green);

    setTitle("Creating Color Pallete");
  public void actionPerformed(ActionEvent e)
    c.setBackground(clr);  		      // calling with c is must
    c.repaint();  			      // or simply repaint
  public static void main(String args[])
    new JColorChooserDemo();

Output screen on JColorChooser Example

clr = JColorChooser.showDialog(this, “Choose Your Color”, Color.green);

The fist parameter this refers the parent class JColorChooserDemo, the second refers to the title of the color chooser dialog box and the third one is the default color selected in color chooser.

Leave a Comment

Your email address will not be published.