1. Technology

Color Chooser Program


Java Code

Here is example Java code for showing a color chooser dialog using the JColorChooser class.

When run this program will show a window with a text area, a show dialog button and a create dialog button. The text area provides feedback to what the user is doing. The show dialog button shows a color chooser dialog created from the showDialog method. The create dialog method creates a color chooser dialog through a combination of the JDialog and JColor classes. Each time the OK button is clicked the background color of the text area will change to the selected color.

The article relating to this program is Showing a Color Chooser Dialog.

Note: To run the Java code this program should be saved in a file called ColorChooserProgram.java

//Imports are listed in full to show what's being used
//could just import javax.swing.* and java.awt.* etc..
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.EventQueue;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JColorChooser;

public class ColorChooserProgram {
    JFrame guiFrame;
    JTextArea tracker; 
    JPanel optPanel;
    //Note: Typically the main method will be in a
    //separate class. As this is a simple one class
    //example it's all in the one class.
    public static void main(String[] args) {
         //Use the event dispatch thread for Swing components
         EventQueue.invokeLater(new Runnable()
             public void run()
                 new ColorChooserProgram();         
    public ColorChooserProgram()
        guiFrame = new JFrame();
        //make sure the program exits when the frame closes
        guiFrame.setTitle("Dialog Box Example");
        //This will center the JFrame in the middle of the screen
        guiFrame.setLayout(new BorderLayout());
        //Using a JTextArea to diplay feedback
        tracker = new JTextArea("File Tracker:");
        guiFrame.add(tracker, BorderLayout.NORTH);
        optPanel = new JPanel();
        optPanel.setLayout(new GridLayout(1,2));
        //button for the show dialog method
        JButton showButton = new JButton("Show Color Dialog");
        showButton.setActionCommand("Show Color Dialog");
        showButton.addActionListener(new ActionListener()
            public void actionPerformed(ActionEvent event)
                Color selectedColor = JColorChooser.showDialog(guiFrame, "Pick a Color"
                , Color.GREEN);
                if (selectedColor != null)
                    tracker.append("\nThe selected color is make up of Red: " 
                        + selectedColor.getRed() + " Blue: " + selectedColor.getBlue()
                        + " Green: " + selectedColor.getGreen());
        //button for the create dialog method
        JButton createButton = new JButton("Create Color Dialog");
        createButton.setActionCommand("Create Color Dialog");
        createButton.addActionListener(new ActionListener()
            public void actionPerformed(ActionEvent event)
                //this users a JColorchooser instance in combination
                //with a JDialog to create a color chooser dialog. It's modeless
                //and the OK and Cancel buttons can be listened to.
                final JColorChooser colorChooser = new JColorChooser();
                JDialog dialog = JColorChooser.createDialog(guiFrame, 
                        "Set Text Area color", false, colorChooser
                        , new ActionListener()
                             public void actionPerformed(ActionEvent event)
                               //this actionListenerr is for the OK button
                               tracker.append("\nI can feel my color being changed to "
                                       + colorChooser.getColor());
                        , new ActionListener()
                             public void actionPerformed(ActionEvent event)
                               //this actionListener is for the cancel button
                               tracker.append("\nCancel button clicked..");

  1. About.com
  2. Technology
  3. Java
  4. Create User Interfaces
  5. Using Dialog Boxes
  6. Example Java Code: Color Chooser Program

©2014 About.com. All rights reserved.