Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

JColorChooser – Picking Color

1 About JColorChooser

JColorChooser is a Java SDK’s built-in dialog. Once the dialog is displayed, the user can pick the color from it in many ways. This class exposes a static method showDialog which one can use to display Color picking dialog. The method returns AWT Color object, which is picked by the user. One can use this retrieved AWT color in any of the built-in Java SDK API which looks for color.

2. About the Example

Below is the screenshot of the JColorChooser Example:

Java Swing JColorChooser Example
Java Swing JColorChooser Example

This example has two buttons and a JTextArea. The user can click the Change Background and Change Foreground JButtons to set the background and text color for the text area. These two buttons will display a Java SDK built-in dialog so that user can pick the color from it. The color picking dialog is below:

JColorChooser Dialog
JColorChooser Dialog

The dialog contains five methods to pick the color. First tab, let the user pick from the fixed color bullets. Other four tabs allow the user to mix the color parameters and generate their own. After picking the color, the user will click the OK button and the dialog will return the AWT Color object to the caller. Note, our application will use this returned value to set the fore and back color of the Text Area. Below screen shows how the text area is set with green foreground and black background color.

JTextArea With Customized Back and Fore Color
JTextArea With Customized Back and Fore Color

3. Create Panels

We create two panels: Top, Middle. We will use the top panel to host two JButtons and a Flow Layout manager takes care of its position. The Middle panel uses the border layout and we will use it to fill the entire container with the JTextArea.

4. Prepare Top Panel

Two JButtons sit in the top panel, which is managed by flow layout. We will handle the button clicks later.

5. Prepare JTextArea

We have knowledge of the JTextArea and what is the need for the JScrollPane. Here, in the below code, we add JScrollPane which scrolls the JTextArea to the Middle Panel.

6. Populate ContentPane

Now both the panels are ready. Next, we add them to the JFrame Window, which is managed by the border layout. Top panel is added to the north of the border layout and text area is added to the middle of the content pane.

7. JColorChooser Get Selected Color

The below code is for Background Color button click. It is an anonymous handler for ActionEvent. The call to the method JColorChooser.showDialog will display the color picker dialog. First param takes owner of the dialog and in our case, it is the JFrame. The second param tells the dialog title. Third param sets the default color when Swing shows the dialog to the user. In our case, we set white color as the default.

The showDialog method returns when the user closes the dialog by hitting OK or Cancel button. We take the return value and give it to the JTextArea::setBackground method call.

In the same way, we display the JColorChooser dialog for setting the fore color of the display text of the JTextArea. The only difference is the method call. Here, we make a call to the JTextArea’s method setForeground. Below is the handler code:

You can also watch the YouTube video below, which explains this example.

8. Youtube Demo

9. Code Reference



Categories: Swing

Tags: , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.