JCheckBox Basics: Creation, Labeling, and State Management
JCheckBox Basics: Creation, Labeling, and State Management
JCheckBox Introduction
जब हम Java Swing में user interface (UI) बनाते हैं, तो कई बार हमें ऐसे option देने होते हैं जिन्हें user "select" या "unselect" कर सके। ऐसे cases में JCheckBox सबसे उपयोगी component होता है। JCheckBox एक GUI element है जो "Yes/No" या "True/False" जैसे options को represent करता है। इसका main use होता है जब एक user एक से ज़्यादा options चुन सकता है — जैसे "Sports", "Music", "Reading" आदि।
JCheckBox, Swing package का हिस्सा है और इसे javax.swing.JCheckBox class से create किया जाता है। ये AWT के Checkbox का advanced version है जिसमें look and feel modern है और event handling आसान होती है।
Creating JCheckBox
JCheckBox बनाना बहुत आसान है। इसे बनाने के लिए हम JCheckBox class का object create करते हैं और उसके अंदर label text देते हैं। JCheckBox by default unselected होता है, लेकिन चाहें तो हम उसे selected state में भी initialize कर सकते हैं।
Syntax
JCheckBox checkBox = new JCheckBox("Label Text");
Example Code
import javax.swing.*;
public class CheckBoxExample {
public static void main(String args[]) {
JFrame frame = new JFrame("JCheckBox Example");
JCheckBox checkBox1 = new JCheckBox("C++");
JCheckBox checkBox2 = new JCheckBox("Java", true); // by default selected
checkBox1.setBounds(100,100,100,30);
checkBox2.setBounds(100,150,100,30);
frame.add(checkBox1);
frame.add(checkBox2);
frame.setSize(400,400);
frame.setLayout(null);
frame.setVisible(true);
}
}
ऊपर दिए गए example में दो JCheckBox बनाए गए हैं — एक "C++" और दूसरा "Java"। "Java" वाला checkbox शुरू से ही selected है क्योंकि हमने उसे true parameter दिया है।
Labeling JCheckBox
हर JCheckBox को user के लिए समझने योग्य बनाने के लिए उस पर label लगाया जाता है। ये label बताता है कि checkbox किस option को represent कर रहा है। Label set करने के लिए हम constructor में text पास कर सकते हैं या बाद में setText() method का use कर सकते हैं।
Example of Label Setting
JCheckBox check1 = new JCheckBox();
check1.setText("Accept Terms and Conditions");
Label न केवल user को जानकारी देता है बल्कि accessibility के लिए भी helpful होता है। इसलिए हमेशा meaningful और short label use करना चाहिए।
Dynamic Label Update
कई बार हमें runtime पर JCheckBox का label बदलना होता है — जैसे किसी event के बाद या user input के अनुसार। इसके लिए setText() method का use किया जाता है।
check1.setText("Subscribe to Newsletter");
इसी तरह हम getText() method से current label प्राप्त कर सकते हैं।
State Management in JCheckBox
JCheckBox की सबसे खास बात है उसका “state” यानी कि checkbox selected है या नहीं। इस state को manage करने के लिए कुछ महत्वपूर्ण methods दिए गए हैं जैसे — isSelected(), setSelected() आदि।
Important Methods for State Management
| Method | Description |
|---|---|
isSelected() | Check करता है कि checkbox selected है या नहीं। |
setSelected(boolean state) | Checkbox की state manually set करता है। |
getText() | Current label text return करता है। |
setText(String text) | Label text change करता है। |
Example – Checking State
import javax.swing.*;
import java.awt.event.*;
public class CheckBoxStateExample {
public static void main(String args[]) {
JFrame frame = new JFrame("CheckBox State Example");
JCheckBox cb1 = new JCheckBox("C Programming");
JCheckBox cb2 = new JCheckBox("Python");
cb1.setBounds(100,100,150,30);
cb2.setBounds(100,150,150,30);
JButton b = new JButton("Show Selected");
b.setBounds(100,200,150,30);
JLabel label = new JLabel("");
label.setBounds(100,250,250,30);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String msg = "";
if(cb1.isSelected()){
msg += cb1.getText() + " ";
}
if(cb2.isSelected()){
msg += cb2.getText();
}
label.setText("Selected: " + msg);
}
});
frame.add(cb1); frame.add(cb2); frame.add(b); frame.add(label);
frame.setSize(400,400);
frame.setLayout(null);
frame.setVisible(true);
}
}
ऊपर के example में, जब button क्लिक किया जाता है तो program ये check करता है कि कौन-कौन से checkboxes selected हैं और उन्हें label में display करता है। यही JCheckBox का core use है — multiple selection manage करना।
Event Handling with JCheckBox
JCheckBox में event handling बहुत important होती है क्योंकि हमें यह जानना होता है कि user ने checkbox select या deselect किया है। इसके लिए हम ItemListener या ActionListener का use कर सकते हैं।
Example Using ItemListener
import javax.swing.*;
import java.awt.event.*;
public class CheckBoxEventExample {
public static void main(String args[]) {
JFrame frame = new JFrame("Event Handling Example");
JCheckBox checkBox = new JCheckBox("Enable Dark Mode");
checkBox.setBounds(100,100,200,30);
JLabel label = new JLabel("Dark Mode: OFF");
label.setBounds(100,150,200,30);
checkBox.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange()==1)
label.setText("Dark Mode: ON");
else
label.setText("Dark Mode: OFF");
}
});
frame.add(checkBox);
frame.add(label);
frame.setSize(400,400);
frame.setLayout(null);
frame.setVisible(true);
}
}
ऊपर के example में जैसे ही user checkbox select करता है, label का text बदल जाता है — “Dark Mode: ON” या “OFF”। इससे UI में interactivity बढ़ जाती है।
Appearance Customization
JCheckBox का look customize करना भी आसान है। हम background color, foreground color, font और icon change कर सकते हैं ताकि UI attractive दिखे।
Common Appearance Methods
| Method | Use |
|---|---|
setBackground(Color c) | Background color change करता है। |
setForeground(Color c) | Text color change करता है। |
setFont(Font f) | Text font और size change करता है। |
setIcon(Icon icon) | Custom icon assign करता है। |
Example with Customization
JCheckBox cb = new JCheckBox("Email Notifications");
cb.setBackground(Color.lightGray);
cb.setForeground(Color.blue);
cb.setFont(new Font("Arial", Font.BOLD, 14));
Customization से न केवल UI बेहतर दिखती है बल्कि user experience भी smooth रहता है। यह खासकर desktop applications में useful होता है।
Real-Life Use Cases of JCheckBox
JCheckBox का इस्तेमाल कई जगहों पर होता है, जैसे —
- Registration forms में multiple interests select करने के लिए
- Settings panels में features enable/disable करने के लिए
- Quiz या survey forms में multiple answer options के लिए
- Subscription preferences में newsletters select करने के लिए
इन सभी जगहों पर JCheckBox flexible और interactive experience देता है।
Advantages of JCheckBox
- Multiple selection allow करता है।
- Simple event handling mechanism देता है।
- Customizable appearance support करता है।
- Lightweight और responsive है।
- Swing के बाकी components के साथ compatible है।
Short Notes for Exam
| Topic | Notes |
|---|---|
| Class | javax.swing.JCheckBox |
| Parent Class | JToggleButton |
| Package | javax.swing |
| Constructor | JCheckBox(), JCheckBox(String text), JCheckBox(String text, boolean state) |
| Main Methods | isSelected(), setSelected(), setText(), getText() |
| Event Listener | ItemListener, ActionListener |
| Use | Multiple selection options provide करना |
| Example | “Select subjects”, “Enable notifications” |
Key Points to Remember
- JCheckBox, Swing का important component है जो multiple selection allow करता है।
- यह
JToggleButtonclass को extend करता है। - State management के लिए
isSelected()औरsetSelected()सबसे ज़रूरी methods हैं। - Event handling के लिए
ItemListenercommonly use किया जाता है। - UI customization के लिए colors, fonts और icons change किए जा सकते हैं।