Program to demonstrate how to use JColorChooser in Java Swing
package com.hubberspot.swing;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
class JColorChooserFrame extends JFrame {
private JPanel panel;
private JButton button;
private Color color = Color.BLUE;
public JColorChooserFrame() {
setTitle("JColorChooser Test Frame");
panel = new JPanel();
panel.setBackground(color);
button = new JButton();
button.setText("Select Color");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
color = JColorChooser.showDialog(null, "Pick a color", color);
panel.setBackground(color);
}
});
add(panel,BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);
}
}
public class JColorChooserDemo {
public static void main(String[] args) {
JColorChooserFrame frame = new JColorChooserFrame();
frame.setSize(450, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Output of the program :


