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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package com.mycompany.guiexamples; import java.awt.Color; import java.awt.Font; import javax.swing.*; /** * * @author Fawadkhan */ public class JCheckBoxExample { public static void main(String[] args) { JFrame myJFrame = new JFrame(); myJFrame.setSize(450, 300); myJFrame.setTitle("JCheckBox Testing example"); JPanel panel = new JPanel(); JLabel label = new JLabel("Courses"); label.setFont(new Font("Calibri", Font.BOLD, 25)); panel.setBackground(Color.green); panel.add(label); //JCheckBoxes are created JCheckBox checkBoxJava = new JCheckBox("Java"); JCheckBox checkBoxCPlusPlus = new JCheckBox("C++"); JCheckBox checkBoxCsharp = new JCheckBox("C#"); JCheckBox checkBoxPHP = new JCheckBox("PHP"); JCheckBox checkBoxJavaScript = new JCheckBox("JavaScript"); //JCheckBoxes are added to panel.add(checkBoxJava); panel.add(checkBoxCPlusPlus); panel.add(checkBoxCsharp); panel.add(checkBoxPHP); panel.add(checkBoxJavaScript); myJFrame.add(panel); myJFrame.setVisible(true); } } |
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:
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.
1 |
setContentAreaFilled(boolean b); |
this method is is used to remove the default background color of the java JCheckBox.
1 |
setBorderPainted(boolean b); |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package com.mycompany.guiexamples; import java.awt.Color; import java.awt.Font; import javax.swing.*; /** * * @author Fawadkhan */ public class JCheckBoxExample { public static void main(String[] args) { JFrame myJFrame = new JFrame(); myJFrame.setSize(450, 300); myJFrame.setTitle("JCheckBox Testing example"); JPanel panel = new JPanel(); JLabel label = new JLabel("Courses"); label.setFont(new Font("Calibri", Font.BOLD, 25)); panel.setBackground(Color.green); panel.add(label); //JCheckBoxes are created JCheckBox checkBoxJava = new JCheckBox("Java"); checkBoxJava.setContentAreaFilled(false); JCheckBox checkBoxCPlusPlus = new JCheckBox("C++"); checkBoxCPlusPlus.setContentAreaFilled(false); JCheckBox checkBoxCsharp = new JCheckBox("C#"); checkBoxCsharp.setContentAreaFilled(false); JCheckBox checkBoxPHP = new JCheckBox("PHP"); checkBoxPHP.setContentAreaFilled(false); JCheckBox checkBoxJavaScript = new JCheckBox("JavaScript"); checkBoxJavaScript.setContentAreaFilled(false); //JCheckBoxes are added to panel.add(checkBoxJava); panel.add(checkBoxCPlusPlus); panel.add(checkBoxCsharp); panel.add(checkBoxPHP); panel.add(checkBoxJavaScript); myJFrame.add(panel); myJFrame.setVisible(true); } } |
Output:
Example: how to set the 3d border of the java JCheckbox using NetBeans:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
package com.mycompany.guiexamples; import java.awt.Color; import java.awt.Font; import javax.swing.*; /** * * @author Fawadkhan */ public class JCheckBoxExample { public static void main(String[] args) { JFrame myJFrame = new JFrame(); myJFrame.setSize(450, 300); myJFrame.setTitle("JCheckBox Testing example"); JPanel panel = new JPanel(); JLabel label = new JLabel("Courses"); label.setFont(new Font("Calibri", Font.BOLD, 25)); panel.setBackground(Color.green); panel.add(label); //JCheckBoxes are created JCheckBox checkBoxJava = new JCheckBox("Java"); checkBoxJava.setContentAreaFilled(false); checkBoxJava.setBorderPainted(true); JCheckBox checkBoxCPlusPlus = new JCheckBox("C++"); checkBoxCPlusPlus.setContentAreaFilled(false); checkBoxCPlusPlus.setBorderPainted(true); JCheckBox checkBoxCsharp = new JCheckBox("C#"); checkBoxCsharp.setContentAreaFilled(false); checkBoxCsharp.setBorderPainted(true); JCheckBox checkBoxPHP = new JCheckBox("PHP"); checkBoxPHP.setContentAreaFilled(false); checkBoxPHP.setBorderPainted(true); JCheckBox checkBoxJavaScript = new JCheckBox("JavaScript"); checkBoxJavaScript.setContentAreaFilled(false); checkBoxJavaScript.setBorderPainted(true); //JCheckBoxes are added to panel.add(checkBoxJava); panel.add(checkBoxCPlusPlus); panel.add(checkBoxCsharp); panel.add(checkBoxPHP); panel.add(checkBoxJavaScript); myJFrame.add(panel); myJFrame.setVisible(true); } } |
Output:
Example: Java JCheckBox with ItemListener using Netbeans:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
package com.mycompany.guiexamples; import java.awt.Color; import java.awt.Font; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.*; /** * * @author Fawadkhan */ public class JCheckBoxExample { public static void main(String[] args) { JFrame myJFrame = new JFrame(); myJFrame.setSize(430, 280); myJFrame.setTitle("JCheckBox Testing example"); JPanel panel = new JPanel(); JLabel label = new JLabel("Courses"); label.setFont(new Font("Calibri", Font.BOLD, 25)); panel.setBackground(Color.green); panel.add(label); //JCheckBoxes are created JCheckBox checkBoxJava = new JCheckBox("Java"); checkBoxJava.setContentAreaFilled(false); JCheckBox checkBoxCPlusPlus = new JCheckBox("C++"); checkBoxCPlusPlus.setContentAreaFilled(false); JCheckBox checkBoxCsharp = new JCheckBox("C#"); checkBoxCsharp.setContentAreaFilled(false); JCheckBox checkBoxPHP = new JCheckBox("PHP"); checkBoxPHP.setContentAreaFilled(false); JCheckBox checkBoxJavaScript = new JCheckBox("JavaScript"); checkBoxJavaScript.setContentAreaFilled(false); //JCheckBoxes are added to panel.add(checkBoxJava); panel.add(checkBoxCPlusPlus); panel.add(checkBoxCsharp); panel.add(checkBoxPHP); panel.add(checkBoxJavaScript); JLabel label2 = new JLabel(""); label2.setHorizontalAlignment(JLabel.CENTER); label2.setFont(new Font("Calibri", Font.BOLD, 25)); label2.setForeground(Color.red); panel.add(label2); checkBoxJava.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { label2.setText("Java Checkbox: " + (e.getStateChange() == 1 ? "checked" : "unchecked")); } }); checkBoxCPlusPlus.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { label2.setText("C++ Checkbox: " + (e.getStateChange() == 1 ? "checked" : "unchecked")); } }); checkBoxCsharp.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { label2.setText("C# Checkbox: " + (e.getStateChange() == 1 ? "checked" : "unchecked")); } }); checkBoxPHP.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { label2.setText("PHP Checkbox: " + (e.getStateChange() == 1 ? "checked" : "unchecked")); } }); checkBoxJavaScript.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { label2.setText("JavaScript Checkbox: " + (e.getStateChange() == 1 ? "checked" : "unchecked")); } }); myJFrame.add(panel); myJFrame.setVisible(true); } } |
Output: