All for Joomla All for Webmasters

JColorChooser class

The JColorChooser class is used to create a color chooser dialog box so that user can select any color.

Commonly used Constructors of JColorChooser class:

  • 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.

color chooser java

Commonly used methods of JColorChooser class:

public static Color showDialog(Component c, String title, Color initialColor): is used to show the color-chooser dialog box.
  1. import java.awt.event.*;
  2. import java.awt.*;
  3. import javax.swing.*;
  4. public class JColorChooserExample extends JFrame implements ActionListener{
  5. JButton b;
  6. Container c;
  7. JColorChooserExample(){
  8.     c=getContentPane();
  9.     c.setLayout(new FlowLayout());
  10.     b=new JButton(“color”);
  11.     b.addActionListener(this);
  12.     c.add(b);
  13. }
  14. public void actionPerformed(ActionEvent e) {
  15. Color initialcolor=Color.RED;
  16. Color color=JColorChooser.showDialog(this,“Select a color”,initialcolor);
  17. c.setBackground(color);
  18. }
  19. public static void main(String[] args) {
  20.     JColorChooserExample ch=new JColorChooserExample();
  21.     ch.setSize(400,400);
  22.     ch.setVisible(true);
  23.     ch.setDefaultCloseOperation(EXIT_ON_CLOSE);
  24. }
  25. }
submit to reddit

Leave a Reply