Working With Color Picker in Swing

Introduction

 
In this article, we describe how we can work with the color-chooser. The color chooser dialog box is a very general thing for GUI applications. Java provides a JColorChooser class with the Swing package. Use the JColorChooser class to enable users to choose from a palette of colors. A color chooser is a component that you can place anywhere within your GUI program. The JColorChooser API also makes it easy to bring up a dialog (modal or not) that contains a color-chooser.
 
For adding a color chooser to your application you have to follow some steps.
 
Step 1: Import the necessary package.
  1. import javax.swing.JDialog;  
  2. import javax.swing.JFrame;  
  3. import javax.swing.JPanel;  
  4. import javax.swing.JButton;  
  5. import javax.swing.JTextArea;  
  6. import java.awt.EventQueue;  
  7. import java.awt.BorderLayout;  
  8. import java.awt.Color;  
  9. import java.awt.GridLayout;  
  10. import java.awt.event.ActionListener;  
  11. import java.awt.event.ActionEvent;  
  12. import javax.swing.JColorChooser;  
Step 2: Create a class and define the component variables.
  1. public class ColorChooserDemo  
  2. {  
  3.     JFrame myFrame;  
  4.     JTextArea ta;  
  5.     JPanel pnl;  
  6.     Step 3: Create a GUI within the constructor.  
  7.     public ColorChooserDemo()  
  8.     {  
  9.         myFrame = new JFrame();  
  10.         // for handle the frame close operation  
  11.         myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  12.         //set the title of frame  
  13.         myFrame.setTitle("Color choosher Dialog box Example");  
  14.         //Set the size  
  15.         myFrame.setSize(500300);  
  16.         myFrame.setLocationRelativeTo(null);  
  17.         myFrame.setLayout(new BorderLayout());  
  18.         // creating TextArea  
  19.         ta = new JTextArea("Your TextArea:");  
  20.         ta.setVisible(true);  
  21.         //Add text Area in frame  
  22.         myFrame.add(ta, BorderLayout.NORTH);  
  23.         // creating panel  
  24.         pnl = new JPanel();  
  25.         //pass the object of grid layout  
  26.         pnl.setLayout(new GridLayout(12));  
  27.         myFrame.add(pnl, BorderLayout.SOUTH);  
  28.         //creating A button  
  29.         JButton showButton = new JButton("Show ColorChooser box");  
  30.         showButton.setActionCommand("Show ColorChooser box");  
  31.         //Performing event handling  
Step 3: Perform event handling. 
  1. showButton.addActionListener(new ActionListener()  
  2.     {  
  3.         public void actionPerformed(ActionEvent event)  
  4.         {  
  5.             Color selectedColor = JColorChooser.showDialog(myFrame, "Pick a Color", Color.GREEN);  
  6.             if (selectedColor != null)  
  7.             {  
  8.                 ta.append("\nThe selected color is make up of Red: " + selectedColor.getRed() + " Blue: " + selectedColor.getBlue() + " Green: " + selectedColor.getGreen());  
  9.             }  
  10.         }  
  11.     });  
  12. pnl.add(showButton);  
  13. JButton createButton = new JButton("Create Color");  
  14. createButton.setActionCommand("Create Color");  
  15. createButton.addActionListener(new ActionListener()  
  16.     {  
  17.         @Override  
  18.         public void actionPerformed(ActionEvent event)  
  19.         {  
  20.             final JColorChooser colorChooser = new JColorChooser();  
  21.             JDialog dialog = JColorChooser.createDialog(myFrame, "Chane TextArea color"false, colorChooser, new ActionListener()  
  22.                 {  
  23.                     @Override  
  24.                     public void actionPerformed(ActionEvent event)  
  25.                     {  
  26.                         ta.append("\n Now my color is changed to " + colorChooser.getColor());  
  27.                         ta.setBackground(colorChooser.getColor());  
  28.                     }  
  29.                 }  
  30.                 , new ActionListener()  
  31.                 {  
  32.                     @Override  
  33.                     public void actionPerformed(ActionEvent event)  
  34.                     {  
  35.                         ta.append("\nYou  clicked on cancel..");  
  36.                     }  
  37.                 });  
  38.             dialog.setVisible(true);  
  39.         }  
  40.     });  
  41. pnl.add(createButton);  
  42. myFrame.setVisible(true);  
  43. }  
Step 4: All the GUI-related components are in the constructor.
 
Complete code
  1. import javax.swing.JDialog;  
  2. import javax.swing.JFrame;  
  3. import javax.swing.JPanel;  
  4. import javax.swing.JButton;  
  5. import javax.swing.JTextArea;  
  6. import java.awt.EventQueue;  
  7. import java.awt.BorderLayout;  
  8. import java.awt.Color;  
  9. import java.awt.GridLayout;  
  10. import java.awt.event.ActionListener;  
  11. import java.awt.event.ActionEvent;  
  12. import javax.swing.JColorChooser;  
  13. public class ColorChooserDemo {  
  14.     JFrame myFrame;  
  15.     JTextArea ta;  
  16.     JPanel pnl;  
  17.     public ColorChooserDemo() {  
  18.         myFrame = new JFrame();  
  19.         myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  20.         myFrame.setTitle("Dialog Box Example");  
  21.         myFrame.setSize(500300);  
  22.         myFrame.setLocationRelativeTo(null);  
  23.         myFrame.setLayout(new BorderLayout());  
  24.         ta = new JTextArea("File ta:");  
  25.         ta.setVisible(true);  
  26.         myFrame.add(ta, BorderLayout.NORTH);  
  27.         pnl = new JPanel();  
  28.         pnl.setLayout(new GridLayout(12));  
  29.         myFrame.add(pnl, BorderLayout.SOUTH);  
  30.         JButton showButton = new JButton("Show ColorChooser box");  
  31.         showButton.setActionCommand("Show ColorChooser box");  
  32.         showButton.addActionListener(new ActionListener() {  
  33.             public void actionPerformed(ActionEvent event) {  
  34.                 Color selectedColor = JColorChooser.showDialog(myFrame, "Pick a Color", Color.GREEN);  
  35.                 if (selectedColor != null) {  
  36.                     ta.append("\nThe selected color is make up of Red: " + selectedColor.getRed() + " Blue: " + selectedColor.getBlue() + " Green: " + selectedColor.getGreen());  
  37.                 }  
  38.             }  
  39.         });  
  40.         pnl.add(showButton);  
  41.         JButton createButton = new JButton("Create Color");  
  42.         createButton.setActionCommand("Create Color");  
  43.         createButton.addActionListener(new ActionListener() {  
  44.             @Override  
  45.             public void actionPerformed(ActionEvent event) {  
  46.                 final JColorChooser colorChooser = new JColorChooser();  
  47.                 JDialog dialog = JColorChooser.createDialog(myFrame, "Chane TextArea color"false, colorChooser,  
  48.                     new ActionListener() {  
  49.                         @Override  
  50.                         public void actionPerformed(ActionEvent event) {  
  51.                             ta.append("\n Now my color is changed to " + colorChooser.getColor());  
  52.                             ta.setBackground(colorChooser.getColor());  
  53.                         }  
  54.                     }, new ActionListener() {  
  55.                         @Override  
  56.                         public void actionPerformed(ActionEvent event) {  
  57.                             ta.append("\nYou  clicked on cancel..");  
  58.                         }  
  59.                     });  
  60.                 dialog.setVisible(true);  
  61.             }  
  62.         });  
  63.         pnl.add(createButton);  
  64.         myFrame.setVisible(true);  
  65.     }  
  66.     public static void main(String[] args)  
  67.     {  
  68.         EventQueue.invokeLater(new Runnable()  
  69.             {  
  70.                 @Override  
  71.                 public void run()  
  72.                 {  
  73.                     new ColorChooserDemo();  
  74.                 }  
  75.             });  
  76.     }  
  77. }  
Output
 
Cmd output
 
colorchooser00.jpg 
 
Initial look (blank output):
 
colorchooser0.jpg 
 
After clicking on the show ColorChooser box, the following color chooser dialog box is opened.
 
colorchooser1.jpg 
 
After click on create color button and select HSB option of the color chooser dialog box.
 
colorchooser2.jpg 
 
Now click on the ok button; your text area color has been changed.
 
colorchooser3.jpg 
 
Resources  


Similar Articles