Username:
Password:
Java 6 Tutorials

Swing Components / Choosing Colors

Subtitles of the Movie

You have two ways that you can ask the user to choose a color. Which of the two you use depends on the needs of your application. Let me first show you the color chooser dialog. The main window of this example has two buttons. They both pop up a dialog that allows for a color choice. The color return from the first button changes the background color of the buttons. The second changes the color of the text in the buttons. Now, here's what happens when you press a button. The text you pass to this static method is the text shown on the bar at the top of the window. The third argument here is the initial color. In this example it's the foreground color of the button, B1. Notice that a color is always returned. If the user cancels the pop up, the initial default color is returned so you always need to set the default color. In the same way, selecting the second button, the button labeled letters will cause the foreground, the lettering of the buttons to be changed. And here's how it works. The main window of the application is nothing but a couple of buttons. We can change the background color of the buttons this way. The first view you get is that of a swatches. These are some commonly used colors for you to choose from, but you can pick the colors in other ways. If you prefer, you can select the three RGB values for your color. You can see the default button color value shown here. It's almost white with all the RGB values close to their maximum. Now, changing the color here also changes the color settings for the other two views of the color. Speaking of the other two views, look at this HSB. The letters HSB stand for hue, saturation and brightness. It's an attempt to display colors in a more intuitive way. It's different and you'll need to fiddle with it a bit to see which one you like best. For this example I'll just pick a swatch. Notice how when you pick a color, examples of it are displayed at the bottom of the window. The examples are displayed next to some black and white areas so you can get a better idea of what it will look like in your application. The reset button always restores the default color. And the recent colors allow you to go back to a color you previously selected. The OK button causes the dialog to return the color that you've currently selected. You can see that his turned the buttons blue. The second button will change the color of the letters. As you can see by the preview, the default color for the letters is almost black. Choosing another color will change the letters. There. They've changed. There is another way you can use the color chooser. It can be included in one of your windows, just like you would include any other component. You will need to import the change listener interface and the change event class and you will need to implement the change listener interface. Now, this example is a window with a label at the top and the color chooser as a component at the bottom. A new color chooser object is constructed with the foreground color of the label as the initial color. Just like with a button, the foreground color is the color of the letters. You want to be notified when a color selection has been made and that is done by the color selection model. So you get the address of the color selection model and add this object to the address of those objects that will be notified when a color selection has been made. This method is called in every time a change of color has been selected. The returned color is then inserted into the label as the new foreground color. It works like this. This is the same color selection as before, but there are no buttons at the bottom. Each time a color is selected, the change shows up immediately with a label at the top.

Tutorial Information

Course: Java 6
Author: Arthur Griffith
SKU: 33858
ISBN: 1-934743-59-3
Release Date: 2008-02-29
Duration: 7 hrs / 92 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 81,350 Video Tutorials (20,800 free)
  • Video Available as Flash or QuickTime
  • Over 782 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available