Monday, 14 September 2015

JColorChooser class

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.

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.

Example of JColorChooser class

JColorChooser example
  1. import java.awt.event.*;  
  2. import java.awt.*;  
  3. import javax.swing.*;  
  4.   
  5. public class JColorChooserExample extends JFrame implements ActionListener{  
  6. JButton b;  
  7. Container c;  
  8.   
  9. JColorChooserExample(){  
  10.     c=getContentPane();  
  11.     c.setLayout(new FlowLayout());  
  12.       
  13.     b=new JButton("color");  
  14.     b.addActionListener(this);  
  15.       
  16.     c.add(b);  
  17. }  
  18.   
  19. public void actionPerformed(ActionEvent e) {  
  20. Color initialcolor=Color.RED;  
  21. Color color=JColorChooser.showDialog(this,"Select a color",initialcolor);  
  22. c.setBackground(color);  
  23. }  
  24.   
  25. public static void main(String[] args) {  
  26.     JColorChooserExample ch=new JColorChooserExample();  
  27.     ch.setSize(400,400);  
  28.     ch.setVisible(true);  
  29.     ch.setDefaultCloseOperation(EXIT_ON_CLOSE);  
  30. }  
  31. }  

No comments:

Post a Comment