JColorChooser class in java

JColorChooser class

This class is useful to create a color choosers dialog box with several colors so that the user can select any color.

Constructors

  • JColorChooser()
  • JColorChooser(Color initialColor)

Methods

  • public static Color showDialog(Component c, String title,
  • Color initialColor)
  • Color getColor()
  • void setColor(Color color)
Example
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class test extends JFrame implements ActionListener
{
JButton b;
Container c;
test()
{
c=getContentPane();
c.setLayout(new FlowLayout());
b=new JButton(“MY Color”);
b.addActionListener(this);
c.add(b);
}
public void actionPerformed(ActionEvent e)
{
Color initialcolor=Color.RED;
Color color=JColorChooser.showDialog(this,”Select color”,initialcolor);
c.setBackground(color);
}
public static void main(String[] args)
{
test t=new test();
t.setSize(400,400);
t.setVisible(true);
t.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

Output:

Untitled

Leave a Reply

Your email address will not be published. Required fields are marked *