Java Swing | JColorChooser Class
JColorChooser provides a pane of controls designed to allow a user to manipulate and select a color. This class provides three levels of API:
- A static convenience method that shows a modal color-chooser dialog and returns the color selected by the user.
- A static convenience method for creating a color-chooser dialog where ActionListeners can be specified to be invoked when the user presses one of the dialog buttons.
- The ability to create instances of JColorChooser panes directly (within any container). PropertyChange listeners can be added to detect when the current “color” property changes.
Constructors of the class:
- 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.
Commonly Used Methods:
|setColor(Color color)||Sets the current color of the color chooser to the specified color.|
|setColor(int c)||Sets the current color of the color chooser to the specified color.|
|setColor(int r, int g, int b)||Sets the current color of the color chooser to the specified RGB color.|
|showDialog(Component cmp, String title, Color init_Color)||Shows a modal color-chooser dialog and blocks until the dialog is hidden.|
|updateUI()||Notification from the UIManager that the L&F has changed
|setChooserPanels(AbstractColorChooserPanel panels)||Specifies the Color Panels used to choose a color value.|
|addChooserPanel(AbstractColorChooserPanel panel)||Adds a color chooser panel to the color chooser.|
|setUI(ColorChooserUI ui)||Sets the L&F object that renders this component.|
|setSelectionModel(ColorSelectionModel newModel)||Sets the model containing the selected color.|
|setPreviewPanel(JComponent preview)||Sets the current preview panel.|
Creating a Custom Chooser Panel: The default color chooser provides five chooser panels:
- Swatches: For choosing a color from a collection of swatches.
- HSV: For choosing a color using the Hue-Saturation-Value color representation. Prior to JDK 7, It was known as HSB, for Hue-Saturation-Brightness.
- HSL: For choosing a color using the Hue-Saturation-Lightness color representation.
- RGB: For choosing a color using the Red-Green-Blue color model.
- CMYK: For choosing a color using the process color or four color model.
Below programs illustrate the use of JColorChooser class:
1. Java program to implement JColorChooser class using ChangeListener: In this program, we first create a label at the top of the window where some text is shown in which we will apply color changes. Set the foreground and background color. Set the size and type of the font. Create a Panel and set its layout. Now set up the color chooser for setting text color. Using stateChanged() method, event is generated for change in color of the text by using getColor() method. Now create the GUI, create a setup window. Set the default close operation of the window. Create and set up the content Pane and add content to the frame and display the window.
2. Java program to implement JColorChooser class using ActionListener: Create a button and a container and set the Layout of the container. Add ActionListener() to the button and add a button to the container. ActionListerner() has one method actionPerformed() which is implemented as soon as the button is clicked. Choose the color and set the background color of the container.
Note: The above programs might not run in an online IDE. Please use an offline compiler.