Header javaperspective.com
JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.28. Color choosers

5.28. Color choosers
Last updated: 1 January 2013.

A color chooser allows the user to select a color. This tutorial will show you how to use color chooser dialogs with the class JColorChooser. A color chooser dialog looks something like this:


Color chooser


The class JColorChooser provides convenient static methods to display color chooser dialogs:
The easiest way to display a modal color chooser dialog is to call the method showDialog as shown in the sample below:

Color selectedColor = JColorChooser.showDialog(parent, "Color selection", Color.BLUE);

if(selectedColor != null){
   
// Do something with the color
}
else{
   
// The user has pressed cancel or has closed the dialog
}

In the above sample, parent is the owner of the dialog which is usually a JFrame instance, although it can be any subclass of the class Component. The dialog is hidden if the user presses OK or Cancel. The selected color is returned if the user has pressed OK and null is returned if the user has pressed Cancel or has closed the dialog.

If that behaviour does not suit your application, you can use the method createDialog which allows you to specify action listeners for each of the buttons OK and Cancel.


You are here :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.28. Color choosers
Next tutorial :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.29. Sliders

Copyright © 2013. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7