SEARCH HERE

Tuesday, December 27, 2022

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.

JColorChooser class declaration

Let's see the declaration for javax.swing.JColorChooser class.

  1. public class JColorChooser extends JComponent implements Accessible  

Commonly used Constructors:

ConstructorDescription
JColorChooser()It is used to create a color chooser panel with white color initially.
JColorChooser(color initialcolor)It is used to create a color chooser panel with the specified color initially.

Commonly used Methods:

MethodDescription
void addChooserPanel(AbstractColorChooserPanel panel)It is used to add a color chooser panel to the color chooser.
static Color showDialog(Component c, String title, Color initialColor)It is used to show the color chooser dialog box.

Java JColorChooser Example

  1. import java.awt.event.*;    
  2. import java.awt.*;    
  3. import javax.swing.*;     
  4. public class ColorChooserExample extends JFrame implements ActionListener {    
  5. JButton b;    
  6. Container c;    
  7. ColorChooserExample(){    
  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.     
  20. public static void main(String[] args) {    
  21.     ColorChooserExample ch=new ColorChooserExample();    
  22.     ch.setSize(400,400);    
  23.     ch.setVisible(true);    
  24.     ch.setDefaultCloseOperation(EXIT_ON_CLOSE);    
  25. }    
  26. }    

Output:

JAVA Jcolorchooser 1

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:

JAVA Jcolorchooser 2 JAVA Jcolorchooser 3

0 comments:

Post a Comment

C++

AJAVA

C

E-RESOURCES

LKG, UKG Live Worksheets

Top