SWING - JColorChooser Class

The class JColorChooser provides a pane of controls designed to allow a user to manipulate and select a color.
Following is the declaration for javax.swing.JColorChooser class −
public class JColorChooser
   extends JComponent
      implements Accessible

Following are the fields for javax.swing.JLabel class −
protected AccessibleContext accessibleContext
protected AccessibleContext accessibleContext
static String CHOOSER_PANELS_PROPERTY − The chooserPanel array property name.
static String CHOOSER_PANELS_PROPERTY − The chooserPanel array property name.
static String PREVIEW_PANEL_PROPERTY − The preview panel property name.
static String PREVIEW_PANEL_PROPERTY − The preview panel property name.
static String SELECTION_MODEL_PROPERTY − The selection model property name.
static String SELECTION_MODEL_PROPERTY − The selection model property name.
JColorChooser()
Creates a color chooser pane with an initial color of white.
JColorChooser(Color initialColor)
Creates a color chooser pane with the specified initial color.
JColorChooser(ColorSelectionModel model)
Creates a color chooser pane with the specified ColorSelectionModel.
void addChooserPanel(AbstractColorChooserPanel panel)
Adds a color chooser panel to the color chooser.
static JDialog	createDialog(Component c, String title, boolean modal, JColorChooser chooserPane, ActionListener okListener, ActionListener cancelListener)
Creates and returns a new dialog containing the specified ColorChooser pane along with "OK", "Cancel", and "Reset" buttons.
AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this JColorChooser.
AbstractColorChooserPanel[] getChooserPanels()
Returns the specified color panels.
Color getColor()
Gets the current color value from the color chooser.
boolean getDragEnabled()
Gets the value of the dragEnabled property.
JComponent getPreviewPanel()
Returns the preview panel that shows a chosen color.
ColorSelectionModel getSelectionModel()
Returns the data model that handles color selections.
ColorChooserUI getUI()
Returns the L&F object that renders this component.
String getUIClassID()
Returns the name of the L&F class that renders this component.
protected String paramString()
Returns a string representation of this JColorChooser.
AbstractColorChooserPanel removeChooserPanel(AbstractColorChooserPanel panel)
Removes the Color Panel specified.
void setChooserPanels(AbstractColorChooserPanel[] panels)
Specifies the Color Panels used to choose a color value.
void setColor(Color color)
Sets the current color of the color chooser to the specified color.
void setColor(int c)
Sets the current color of the color chooser to the specified color.
void setColor(int r, int g, int b)
Sets the current color of the color chooser to the specified RGB color.
void setDragEnabled(boolean b)
Sets the dragEnabled property, which must be true to enable automatic drag handling (the first part of drag and drop) on this component.
void setPreviewPanel(JComponent preview)
Sets the current preview panel.
void setSelectionModel(ColorSelectionModel newModel)
Sets the model containing the selected color.
void setUI(ColorChooserUI ui)
Sets the L&F object that renders this component.
static Color showDialog(Component component, String title, Color initialColor)
Shows a modal color-chooser dialog and blocks until the dialog is hidden.
void updateUI()
Notification from the UIManager that the L&F has changed.
This class inherits methods from the following classes −
javax.swing.JComponent
java.awt.Container
java.awt.Component
java.lang.Object
Create the following Java program using any editor of your choice in say D:/ > SWING > com > tutorialspoint > gui >
SwingControlDemo.java
package com.tutorialspoint.gui;
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class SwingControlDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public SwingControlDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
      SwingControlDemo  swingControlDemo = new SwingControlDemo();      
      swingControlDemo.showColorChooserDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new JLabel("", JLabel.CENTER);        
      statusLabel = new JLabel("",JLabel.CENTER);    
      statusLabel.setSize(350,100);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }
   private void showColorChooserDemo(){
      headerLabel.setText("Control in action: JColorChooser"); 
      JButton chooseButton = new JButton("Choose Background");        
      
      chooseButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            Color backgroundColor = JColorChooser.showDialog(mainFrame,
               "Choose background color", Color.white);
            if(backgroundColor != null){
               controlPanel.setBackground(backgroundColor);
               mainFrame.getContentPane().setBackground(backgroundColor);
            }
         }
      });
      controlPanel.add(chooseButton);
      mainFrame.setVisible(true);  
   }
}
Compile the program using the command prompt. Go to D:/ > SWING and type the following command.
D:\SWING>javac com\tutorialspoint\gui\SwingControlDemo.java

If no error occurs, it means the compilation is successful. Run the program using the following command.
D:\SWING>java com.tutorialspoint.gui.SwingControlDemo

Verify the following output.

    30 Lectures 
     3.5 hours 

 Pranjal Srivastava

    13 Lectures 
     1 hours 

 Pranjal Srivastava

    25 Lectures 
     4.5 hours 

 Emenwa Global, Ejike IfeanyiChukwu

    14 Lectures 
     1.5 hours 

 Travis Rose

    14 Lectures 
     1 hours 

 Travis Rose
 Print
 Add Notes
 Bookmark this page