Java JCheckBox
JCheckBox class inherits from JToggleButton.It's used to turn a particular option either true or false.
How to Declare JCheckBox class
[public class JCheckBox extends JToggleButton implements Accessible]
JCheckBox commonly used constructors
Below are commonly used JCheckBox constructor.
- JCheckBox()-creates check box button with no text or icon.
- JCheckBox(String s)-creates check box button with a particular text.
- JCheckBox(String s, boolean selected)-creates checkbox with a string that is selected.
- JCheckBox(Action a)-creates check box where properties are taken from the Action supplied.
JCheckBox methods
- [AccessibleContext getAccessibleContext()]-gets accessible context that is related with check box.
- [protected String paramString()]-returns string representation of check box.
See also:Java JPasswordField
Java JCheckBox Example
[
import javax.swing.JFrame;
import javax.swing.JCheckBox; public class JCheckBoxExample extends JFrame{ JCheckBoxExample() { super("JCheckBox Example"); JCheckBox box1 = new JCheckBox("Eclipse IDE"); box1.setBounds(100,100, 100,50); JCheckBox box2 = new JCheckBox("NetBeans", true); box2.setBounds(100,150, 100,50); add(box1); add(box2); setLocationRelativeTo(null); setSize(400,400); setLayout(null); setVisible(true); } public static void main(String args[]) { new JCheckBoxExample(); }
}
]
Output
See also: Java JTextArea
Java JCheckBox Example with ItemListener
[
import javax.swing.JFrame; import javax.swing.JCheckBox; import javax.swing.JLabel; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; public class JCheckBoxExample extends JFrame{ JCheckBoxExample() { super("JCheckBox Example"); JLabel l = new JLabel(""); l.setHorizontalAlignment(JLabel.CENTER); l.setSize(400,100); add(l); JCheckBox box1 = new JCheckBox("Eclipse IDE"); box1.setBounds(100,100, 100,50); box1.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { l.setText("Eclipse IDE: "
+ (e.getStateChange()==1?"checked":"unchecked")); } }); JCheckBox box2 = new JCheckBox("NetBeans", true); box2.setBounds(100,150, 100,50); box2.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { l.setText("NetBeans: "
+ (e.getStateChange()==1?"checked":"unchecked")); } }); add(box1); add(box2); setLocationRelativeTo(null); setSize(400,400); setLayout(null); setVisible(true); } public static void main(String args[]) { new JCheckBoxExample(); } }
]