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:

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:

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.

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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class JColorChooserExample extends JFrame { //Sample 01: Declare a JTextArea JTextArea jta; public JColorChooserExample(String title) throws HeadlessException { super(title); //Sample 02: Set Size and Position setBounds(100, 100, 400, 350); Container ControlHost = getContentPane(); ControlHost.setLayout(new BorderLayout()); //Sample 03: Create Two Panels Panel TopPanel = new Panel(); TopPanel.setLayout(new FlowLayout()); Panel MidPanel = new Panel(); MidPanel.setLayout(new BorderLayout()); |
4. Prepare Top Panel
Two JButtons sit in the top panel, which is managed by flow layout. We will handle the button clicks later.
1 2 3 4 5 |
//Sample 04: Add Two Button to TopPanel JButton btnBG = new JButton("Change Background"); JButton btnFG = new JButton("Change Foreground"); TopPanel.add(btnBG); TopPanel.add(btnFG); |
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.
1 2 3 4 5 6 7 8 9 10 |
//Sample 05: Add a TextArea to MidPanel jta = new JTextArea(30, 50); Font f = new Font("Verdana", Font.PLAIN, 16); jta.setFont(f); jta.setLineWrap(true); jta.setWrapStyleWord(true); JScrollPane JSCPane = new JScrollPane(jta, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); MidPanel.add(JSCPane); |
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.
1 2 3 |
//Sample 06: Populate Content Pane ControlHost.add(TopPanel, BorderLayout.NORTH); ControlHost.add(MidPanel); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
//Sample 07: Use Color Chooser btnBG.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Color BGColor = JColorChooser.showDialog( JColorChooserExample.this, "Pick Background Color For Text Area", Color.WHITE ); jta.setBackground(BGColor); } }); |
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:
1 2 3 4 5 6 7 8 9 10 11 |
btnFG.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Color FGColor = JColorChooser.showDialog( JColorChooserExample.this, "Pick Foreground Color For Text Area", Color.WHITE ); jta.setForeground(FGColor); } }); |
You can also watch the YouTube video below, which explains this example.
8. Youtube Demo
9. Code Reference
9.1 MainEntry.java
1 2 3 4 5 6 7 8 9 10 |
package tube.coding.examples; public class MainEntry { public static void main(String[] args) { //Sample 08: Create Instance of JFrameDemo JColorChooserExample frame = new JColorChooserExample("JColorChooser Example"); frame.setVisible(true); } } |
9.2 JColorChooserExample.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
package tube.coding.examples; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.HeadlessException; import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JColorChooser; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JSeparator; import javax.swing.JTextArea; import javax.swing.JTextField; public class JColorChooserExample extends JFrame { //Sample 01: Declare a JTextArea JTextArea jta; public JColorChooserExample(String title) throws HeadlessException { super(title); //Sample 02: Set Size and Position setBounds(100, 100, 400, 350); Container ControlHost = getContentPane(); ControlHost.setLayout(new BorderLayout()); //Sample 03: Create Two Panels Panel TopPanel = new Panel(); TopPanel.setLayout(new FlowLayout()); Panel MidPanel = new Panel(); MidPanel.setLayout(new BorderLayout()); //Sample 04: Add Two Button to TopPanel JButton btnBG = new JButton("Change Background"); JButton btnFG = new JButton("Change Foreground"); TopPanel.add(btnBG); TopPanel.add(btnFG); //Sample 05: Add a TextArea to MidPanel jta = new JTextArea(30, 50); Font f = new Font("Verdana", Font.PLAIN, 16); jta.setFont(f); jta.setLineWrap(true); jta.setWrapStyleWord(true); JScrollPane JSCPane = new JScrollPane(jta, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); MidPanel.add(JSCPane); //Sample 06: Populate Content Pane ControlHost.add(TopPanel, BorderLayout.NORTH); ControlHost.add(MidPanel); //Sample 07: Use Color Chooser btnBG.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Color BGColor = JColorChooser.showDialog( JColorChooserExample.this, "Pick Background Color For Text Area", Color.WHITE ); jta.setBackground(BGColor); } }); btnFG.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Color FGColor = JColorChooser.showDialog( JColorChooserExample.this, "Pick Foreground Color For Text Area", Color.WHITE ); jta.setForeground(FGColor); } }); } } |
Categories: Swing
Tags: JColorChooser, setBackground, setForeGround, showDialog