Java JCheckBox with Example using NetBeans

Java JCheckBox

A JCheckBox is often used to enable/disable certain functionality in an application. The JCheckBox class derives from the JToggleButton class.


Java JCheckBox Constructors:

In addition to the standard constructor, the JCheckBox class has a few other constructors that we would like to explain in more detail in the following table:

JCheckBox Constructor Description
JCheckBox (Action a) Here the action is assigned directly to a java JCheckBox. 
JCheckBox (Icon icon) This constructor creates a java  JCheckBox object with an Icon.
JCheckBox (String text) Here a java JCheckBox is created with a text as the label.
JCheckBox (String text, Icon icon) Using this constructor, both a text and an icon can be assigned directly to a java JCheckBox object. The text follows in second place after the icon.
JCheckBox (Icon icon, boolean selected) This constructor creates a java JCheckBox with an Icon. In addition, the status of the button is specified. If the value true is passed as the second parameter, the checkbox is selected, i.e. the box contains a tick or a cross.
JCheckBox (String text, boolean selected) Here a java JCheckBox is created with a text as the label. In addition, it is specified which status the checkbox should initially have.
JCheckBox (String text, Icon icon, boolean selected) Here the labeling text, the icon, and the status are set via the constructor.

Below is an example of how a JCheckBox is implemented:



 Example: how to use Java JCheckBox using NetBeans:

We specified the labeling directly when creating the selection boxes. All are set to false by default, they initially do not contain a cross. The execution of the code then gives the following picture:

Java JCheckBox

As you can see from the screenshot above, the actual JCheckBox is slightly larger and has its own background. If you don’t want the JCheckBox with the default background color then you will adjust the background color of your checkbox accordingly.


In addition to the inherited methods, the JCheckBox class has only a few of its own methods.

this method is is used to remove the default background color of the java JCheckBox.

This method can be used to set whether the border of the selection box should be displayed flat or three-dimensionally. If JCheckBoxes are to be used as renderers, for example for cells in a JTable, the setBorderPainted property is usually set to true.

If you want the 3D border, you can set the setBorderPainted(true). However, the extent to which the effect is visible depends on the look and feel that has been set. In the below examples you will learn how to remove the default color and how to set the border of the java JCheckbox.


Example: how to remove the Default Java JCheckbox background color:

Output:

Java JCheckBox


Example: how to set the 3d border of the java JCheckbox using NetBeans:

Output:

Java JCheckBox



Example: Java JCheckBox with ItemListener using Netbeans:

Output:

Java JCheckBox

Java JCheckBox

Recommended For You

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: