JComboBox in Java Swing: Dropdown Mastery with Examples
JComboBox in Java Swing: Dropdown Mastery with Examples
JComboBox in Java Swing क्या है?
Java Swing में JComboBox एक बहुत ही useful component है जो हमें एक dropdown list provide करता है। इस dropdown में user एक option select कर सकता है। जैसे हम website या app में “Select Country” या “Choose Language” dropdown देखते हैं — वही काम JComboBox करता है।
JComboBox का use तब किया जाता है जब हमारे पास multiple options हों और user को उनमें से केवल एक option चुनना हो। यह GUI applications में बहुत ही common और practical element है।
Basic Example
import javax.swing.*;
public class ComboExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JComboBox Example");
String languages[] = {"C", "C++", "Java", "Python"};
JComboBox combo = new JComboBox(languages);
combo.setBounds(50, 50, 150, 30);
frame.add(combo);
frame.setSize(300,200);
frame.setLayout(null);
frame.setVisible(true);
}
}
JComboBox की मुख्य विशेषताएँ
- Dropdown के रूप में multiple options दिखाता है।
- User को केवल एक item चुनने की अनुमति देता है।
- Editable mode में user नया text भी enter कर सकता है।
- Dynamic रूप से items को add या remove किया जा सकता है।
- Event handling के द्वारा selection detect किया जा सकता है।
JComboBox के Constructors
| Constructor | विवरण |
|---|---|
JComboBox() |
एक empty combo box बनाता है। |
JComboBox(Object[] items) |
दी गई array के elements से combo box बनाता है। |
JComboBox(Vector items) |
Vector के elements से combo box बनाता है। |
JComboBox के Common Methods
| Method | विवरण |
|---|---|
addItem(Object item) |
Combo box में नया item जोड़ता है। |
removeItem(Object item) |
दी गई item को combo box से हटाता है। |
getSelectedItem() |
वर्तमान में चुनी गई item को return करता है। |
setSelectedItem(Object item) |
दी गई item को select करता है। |
setEditable(boolean b) |
Combo box को editable बनाता है ताकि user खुद value डाल सके। |
JComboBox में Items Add करने के तरीके
हम items को दो तरह से add कर सकते हैं — constructor के द्वारा या फिर addItem() method से।
Example: Add Items Dynamically
import javax.swing.*;
public class AddItemExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Add Item Example");
JComboBox combo = new JComboBox();
combo.addItem("Red");
combo.addItem("Green");
combo.addItem("Blue");
combo.setBounds(50, 50, 150, 30);
frame.add(combo);
frame.setSize(300,200);
frame.setLayout(null);
frame.setVisible(true);
}
}
Editable JComboBox
By default, JComboBox editable नहीं होता, मतलब user केवल दिए गए options में से चुन सकता है। लेकिन अगर आप चाहते हैं कि user खुद कोई नया value डाल सके, तो setEditable(true) method का use करें।
Example: Editable ComboBox
JComboBox combo = new JComboBox();
combo.addItem("India");
combo.addItem("USA");
combo.addItem("UK");
combo.setEditable(true);
JComboBox में Event Handling
जब user किसी option को select करता है, तो action event generate होता है। इस event को handle करने के लिए हम ActionListener use करते हैं।
Example: Handle Selection Event
import javax.swing.*;
import java.awt.event.*;
public class ComboEventExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Event Example");
String courses[] = {"MCA", "MCA", "B.Tech", "M.Tech"};
JComboBox combo = new JComboBox(courses);
combo.setBounds(50, 50, 150, 30);
JLabel label = new JLabel();
label.setBounds(50, 100, 200, 30);
combo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String selected = (String) combo.getSelectedItem();
label.setText("Selected: " + selected);
}
});
frame.add(combo);
frame.add(label);
frame.setSize(300,200);
frame.setLayout(null);
frame.setVisible(true);
}
}
JComboBox और JList में अंतर
| बिंदु | JComboBox | JList |
|---|---|---|
| Display Type | Dropdown menu के रूप में दिखता है। | पूरी list एक साथ दिखती है। |
| Selection | एक time में एक ही option select होता है। | Multiple selection भी possible है। |
| Editable | Editable बनाया जा सकता है। | Editable नहीं होता। |
| Space | कम space लेता है। | ज्यादा space लेता है। |
JComboBox के फायदे
- User-friendly interface provide करता है।
- Limited screen space में multiple choices देता है।
- Editable होने से user flexibility बढ़ जाती है।
- Programmatically आसानी से modify किया जा सकता है।
JComboBox के Real-Life Uses
- Country या State select करने वाले forms में।
- Language selection menu में।
- Exam applications में subject selection के लिए।
- Software settings या theme selection में।
Exam Point of View से Important Points
- Class: javax.swing.JComboBox
- Package: javax.swing
- Super Class: JComponent
- Implements: ItemSelectable, ListDataListener
- Mostly used in GUI based Java programs।
- Event handling के लिए ActionListener का use होता है।
Short Notes for Exam
- Definition: JComboBox एक dropdown component है जो user को predefined list से एक item चुनने देता है।
- Editable Combo: setEditable(true) से बनाया जाता है।
- Event Listener: ActionListener handle करता है।
- Common Methods: addItem(), getSelectedItem(), removeItem().
- Usage: GUI forms में selection options के लिए।
Quick Summary
- JComboBox Swing का part है जो dropdown functionality provide करता है।
- Dynamic item addition possible है।
- Editable mode user input allow करता है।
- ActionListener event detect करता है।
- Exam में अक्सर इसके constructors और methods पूछे जाते हैं।