Create color chooser dialog

With this tutorial we shall show you how to create a color chooser dialog in a Java Desktop Application. This is a very useful component when you have an application that let’s the user customize the application with colors and we want to make it easy for them to choose the color he wants.

In short, to create a color chooser dialog, one should follow these steps:

  • Create a new JFrame.
  • Use getContentPane().setLayout(new FlowLayout()) to set flow layout for the frame.
  • Create a new JButton that will fire up the file chooser.
  • Add a new ActionListener to the button. Override the actionPerformed method. Now every time the user pressed the button, this method will fire up. Inside this method we are going to create the pop up dialog.
  • To create that dialog, create a new JColorChooser.
  • Use showDialog to pop up the color chooser dialog. The choice will return a Color object.

Let’s see the code snippet that follows:

package com.javacodegeeks.snippets.desktop;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;

public class CreateColorChooserDialog {

	private static final long serialVersionUID = 1L;

	private static void createAndShowGUI() {

		// Create and set up the window.
		final JFrame frame = new JFrame("Centered");

		// Display the window.
		frame.setSize(200, 200);

		// set flow layout for the frame
		frame.getContentPane().setLayout(new FlowLayout());

		JButton button = new JButton("Choose color");
		button.setSize(50, 50);

		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Color color = JColorChooser.showDialog(frame, "Choose a color", Color.blue);
				System.out.println("The selected color was:" + color);



	public static void main(String[] args) {

  //Schedule a job for the event-dispatching thread:

  //creating and showing this application's GUI.

  javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {





This was an example on how to create color chooser dialog in a Java Desktop Application.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Notify of

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

Inline Feedbacks
View all comments
Back to top button