Java JColorChooser Example
Chapter:
Swing
Last Updated:
17-12-2016 09:36:26 UTC
Program:
/* ............... START ............... */
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class JavaJColorChooserExample extends JFrame implements ActionListener {
JButton b;
Container c;
JavaJColorChooserExample() {
c = getContentPane();
c.setLayout(new FlowLayout());
b = new JButton("color");
b.addActionListener(this);
c.add(b);
}
public void actionPerformed(ActionEvent e) {
Color initialcolor = Color.RED;
Color color = JColorChooser.showDialog(this, "Select a color", initialcolor);
c.setBackground(color);
}
public static void main(String[] args) {
JavaJColorChooserExample ch = new JavaJColorChooserExample();
ch.setSize(400, 400);
ch.setVisible(true);
ch.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
/* ............... END ............... */
Output
Notes:
-
The JColorChooser class is used to create a color chooser dialog box so that user can select any color.
- JColorChooser(): is used to create a color chooser pane with white color initially.
- JColorChooser(Color initialColor): is used to create a color chooser pane with the specified color initially.
- public static Color showDialog(Component c, String title, Color initialColor): is used to show the color-chooser dialog box.
Tags
JColorChooser Example, Java, Swing