Swing JColorChooser : Class



package com.c4learn.swing;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JColorChooserDemo {
  private JFrame mainFrame;
  private JPanel mainPanel;
  public JColorChooserDemo() {
    mainFrame = new JFrame("Java Color Chooser");
    mainFrame.setSize(400, 400);
    mainFrame.setLayout(new GridLayout(3, 1));
    mainPanel = new JPanel();
    mainPanel.setLayout(new FlowLayout());
    mainFrame.add(mainPanel);
    mainFrame.setVisible(true);
  }
  public static void main(String[] args) {
    JColorChooserDemo demo = new JColorChooserDemo();
    demo.colorChoose();
  }
  private void colorChoose() {
    JButton chooseBtn = new JButton("Select BG Color");
    chooseBtn.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        Color bgColor = JColorChooser.showDialog(mainFrame,
            "Select Background Color", Color.white);
        if (bgColor != null) {
          mainPanel.setBackground(bgColor);
          mainFrame.getContentPane().setBackground(bgColor);
        }
      }
    });
    mainPanel.add(chooseBtn);
    mainFrame.setVisible(true);
  }
}

Output :

Swing Color Chooser


Swing Color Chooser - Color Selection


Swing Color Chooser - After Color Selection