Java JColorChooser
Java JColorChooser
The JColorChooser class is used to create a color chooser dialog box so that user can
select any color. It inherits JComponent class.
1. public class JColorChooser extends JComponent implements Accessible
Constructor Description
Method Description
static Color showDialog(Component c, String title, Color It is used to show the color chooser
initialColor) dialog box.
Output:
Java JColorChooser Example with ActionListener
1. import javax.swing.*;
2. import java.awt.*;
3. import java.awt.event.*;
4. public class ColorChooserExample extends JFrame implements ActionListener{
5. JFrame f;
6. JButton b;
7. JTextArea ta;
8. ColorChooserExample(){
9. f=new JFrame("Color Chooser Example.");
10. b=new JButton("Pad Color");
11. b.setBounds(200,250,100,30);
12. ta=new JTextArea();
13. ta.setBounds(10,10,300,200);
14. b.addActionListener(this);
15. f.add(b);f.add(ta);
16. f.setLayout(null);
17. f.setSize(400,400);
18. f.setVisible(true);
19. }
20. public void actionPerformed(ActionEvent e){
21. Color c=JColorChooser.showDialog(this,"Choose",Color.CYAN);
22. ta.setBackground(c);
23. }
24. public static void main(String[] args) {
25. new ColorChooserExample();
26. }
27. }
Output: