In this tutorial you will learn about the Swing JCheckBox and its application with practical example.
Swing JCheckBox
JCheckBox which inherits JToggelButton is used to create check box in Swing. This used to turn an option “on” means “true” or “off” means “false”. Clicking on Check Box it means you are changing its state from “off” to “on” or from “on” to “off”.
Constructor Summary: Let’s get more understand about JCheckBox , Some of important constructor are listed below.
JCheckBox() : This constructor construct a new checkbox with unselected state and no text , no icon.
JCheckBox(String text): This constructor construct a new checkbox button with unselected state and with text.
JCheckBox(Icon icon): This constructor construct a new checkbox button with unselected state and with icon.
JCheckBox(String text, boolean selected): This constructor construct a new checkbox with text and specify whether or not it is initially selected.
JCheckBox(String text, Icon icon): This constructor is used to construct a new checkbox button with text and with icon.
JCheckBox(String text, Icon icon, boolean selected): This constructor is used to construct a new checkbox button with text, icon and specify whether or not it is initially selected.
JCheckBox(Action a): Construct a checkbox where properties are taken from the Action supplied.
JCheckBox Method
Some of commonly used methods are listed below. It will help to know more about JCheckBox.
AccessibleContext getAccessibleContext(): Used to gets the AccessibleContext which is associated with this JCheckBox.
void updateUI(): This method is used to resets the UI property to a value from the current look and feel.
protected String paramString(): This method returns string representation of this JCheckBox.
Example :-
An simple example to get understand about JCheckBox. Hopefully this will work for you.
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 |
package w3addaswing; import javax.swing.JCheckBox; import javax.swing.JFrame; public class W3ADDASwing { W3ADDASwing(){ JFrame frame= new JFrame("CheckBox Example W3Adda"); JCheckBox ch_swing = new JCheckBox("Swing"); ch_swing.setBounds(100,100, 80,50); JCheckBox ch_awt = new JCheckBox("AWT", true); ch_awt.setBounds(100,150, 80,50); frame.add(ch_swing); frame.add(ch_awt); frame.setSize(400,400); frame.setLayout(null); frame.setVisible(true); } public static void main(String[] args) { // TODO code application logic here new W3ADDASwing(); } } |
Output :-
Output of this code will look like below image. You just need to copy this code and paste it in you program.