JColorChooser in java With Examples using NetBeans

JColorChooser in java

A color selection dialog is provided by the javax.swing.JColorChooser class. The JColorChooser class can be embedded in its own container or appear in its own dialog. It is used for color selection. Color selection dialogs should be known to most people from other applications (office applications, image editing programs…). There you can, for example, change the text color or color the background with the help of such a dialog. The JColorChooser class only provides the interface for color selection; the desired functionality, i.e. what is done with the selected color, must be implemented by the programmer himself.

Let’s take a look at a simple example where we simply show the JColorChooser in a ready made dialog:

As you can see from the example above, in contrast to the window classes already presented, we do not create an explicit object of the JColorChooser class. The JColorChooser class has a static showDialog method that provides us with a ready-made dialog. This appears when we run the source code above:


When you select the color and press the ok button you will get the following RGB color code in the output


For a better understanding, let’s take a closer look at the showDialog method :

First, we note that the method has the static keyword, so it has been declared static. As we learned in the basics, this means that the method is not bound to an object, but applies to the entire class. Therefore we don’t need to create an object and can call the method directly. The return value of the method is of the Color type (we will go into more detail about the Color class in the following subsection). The first transfer parameter must be of type Component t. This is the base class of all graphical components in Java. What is meant in this case is the parent component. This value can be set when we define the properties of a modal dialog of our class and want to use JColorChooser, as is usually the case in an application. However, in the example above, we passed the null reference because we only want to display the color picker dialog.

showDialog expects a string as the second parameter. The string appears as the title of the color selection dialog. The third parameter is type Color. This is the color that should be selected when opening the dialog. But since we haven’t looked at colors yet, we’ll also pass a null reference here.

The showDialog method can throw a HeadlessException. However, this would only occur in our example if we were running the source code on a system that only supports command line output. Since we’re assuming here that you’re running our example on a graphical user interface operating system, we’ll ignore this exception.

In the example above, we store the return value in the local variable selectedColor. This is either a null reference if the “Cancel” button was pressed or the color value selected in the color selection dialog. Then we output the value of the variable.

This was the simplest example of using JColorChooser.

However, the JColorChooser can also be integrated in a known way by creating and adding an object of the JColorChooser type.

I used a simple notepad for the JFrame and JDialog article, now for  ColorChooser, I am using NetBeans IDE. Furthermore, we have added an import statement for the JColorChooser class. I set the size of our frame to 450 and 300 pixels so that the entire color selection dialog fits into our frame. Then I create an object of the JColorChooser class. Then we get the container that is responsible for the content of our frame using the method getContentPane and add our object for the color selection to it. Then we display the frame. The following screenshot shows our created frame with the added JColorChooser object:


What do we notice immediately when we compare the two screenshots?

We hope you saw that the first screenshot has three additional buttons: “Ok”, “Cancel” and “Reset”. The first sample code allows us to use the color picker directly without much effort. In the second example, however, we would have to implement the buttons and their function ourselves. In the case of the second example, the selected color can only be obtained via the getColor method, which we would have to call via our JColorChooser object.

Color method in java

The java.awt.Color class provides the full spectrum of colors in Java. It has predefined constants for some colors. These constants can be accessed directly using dot notation without having to create an object.

For example, the color black is defined by the constant

The Color class provides constants for the following colors:

The color value is made up of the three primary colors red, green, and blue ( RGB). All other colors are composed of a combination of these primary colors. The table above shows the constants with the associated colors and their RGB values.

Each hue can have a value between 0 and 255 in the RGB representation. In the hexadecimal system, this corresponds to 0 – FF. The color depth is usually specified as 32-bit. If we now look at the maximum hexadecimal value FFFFFF, we only get 24 bits (an F corresponds to 4 bits). You might be wondering where the other 8 bits are “hiding”. This is very easy to explain: These 8 bits stand for the transparency of a color. They are often set constantly to FF (decimal: 255), which means ” fully visible”. Sometimes one also speaks of the alpha or opaque value. A value of 0 would mean that the color is not visible.

The alpha value can be illustrated well-using Teletext from a television. When switching on Teletext, the alpha value for the background is set to 255, ie the background, which is usually black, is fully visible. You can also switch off the background so that you can see the Teletext and the TV picture. The alpha value of the Teletext background has now been set to 0. The alpha value is also important, for example, for watermarks that are often found on images on the Internet. This makes it possible for the watermark to be transparent so that you can still see the image where the watermark was placed. The lower the alpha value of the watermark, the more transparent it is and the more noticeable the background is.

Now let’s look at two constructors of the Color class.

The first constructor expects the color value for the three primary colors red, green, and blue. Full visibility is assumed. The Color class also offers the option of specifying the alpha value explicitly in the second constructor. There are similar constructors with transfer parameters of type float. The values ​​are then converted as follows:

    A float value of 0.0 corresponds to an int value of 0.
    A float value of 1.0 corresponds to an int value of 255

With the brighter and darker methods, Color also offers the option of making an existing color lighter or darker. The methods then return us the modified hue.

Each red, green, blue, and alpha value can be returned via a separate get method.

The class also provides a method, decode, to convert a string representing a 24-bit integer or a hexadecimal value to a color:

This is a static method and returns an object of class Color if possible, otherwise, a NumberFormatException is thrown. A string for the color red could look like “0xFF0000”.

Another method converts a text to color if possible. This method is called getColor and looks like this.

This method looks in the system to see which color is assigned to the passed text. If you passed the string “red”, the constant Color.RED would be returned.

For most applications you develop, the constants for color design will probably be sufficient.


Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button