Feedback Form

Using ButtonGroup: Mutual Exclusivity and Dynamic Management

Using ButtonGroup: Mutual Exclusivity and Dynamic Management

जब हम Java में GUI applications बनाते हैं, तो बहुत बार हमें ऐसे situations मिलते हैं जहाँ एक user को एक ही option चुनना होता है — जैसे gender selection (Male/Female) या payment method (UPI/Card/Cash)। ऐसे cases में ButtonGroup बहुत काम आता है। ये multiple radio buttons को एक group में bind करता है ताकि एक बार में सिर्फ एक ही select किया जा सके। इसे कहते हैं — Mutual Exclusivity

What is ButtonGroup?

ButtonGroup Java Swing का एक class है जो radio buttons को logically group करता है। ये ensure करता है कि एक ही group में से केवल एक radio button active रहे। यानी अगर एक button select होता है तो बाकी automatically deselect हो जाते हैं।

Syntax of ButtonGroup

ButtonGroup को use करने का basic syntax बहुत simple है:

ButtonGroup group = new ButtonGroup(); group.add(radioButton1); group.add(radioButton2); group.add(radioButton3);

ऊपर के code में हमने तीन radio buttons को एक ही group में add किया है ताकि एक समय में सिर्फ एक ही select किया जा सके।

Example: Creating ButtonGroup in Java

चलिए एक छोटा example देखते हैं जहाँ हम तीन radio buttons बनाएंगे — Male, Female और Other — और उन्हें एक ButtonGroup में जोड़ेंगे।

import javax.swing.*; import java.awt.*; public class GenderSelectionExample { public static void main(String[] args) { JFrame frame = new JFrame("Gender Selection Example"); frame.setLayout(new FlowLayout()); JRadioButton male = new JRadioButton("Male"); JRadioButton female = new JRadioButton("Female"); JRadioButton other = new JRadioButton("Other"); ButtonGroup genderGroup = new ButtonGroup(); genderGroup.add(male); genderGroup.add(female); genderGroup.add(other); frame.add(male); frame.add(female); frame.add(other); frame.setSize(300, 150); frame.setVisible(true); } }

इस code में तीनों radio buttons एक ही group में हैं। अब user केवल एक gender option ही select कर पाएगा।

How ButtonGroup Ensures Mutual Exclusivity

Mutual Exclusivity का मतलब है कि एक बार में सिर्फ एक option ही चुना जा सके। जब user किसी एक radio button को click करता है, तो group में बाकी सारे automatically deselect हो जाते हैं। यही behavior real-world forms में भी देखने को मिलता है, जैसे exam form या feedback form।

Internal Working of ButtonGroup

ButtonGroup internally selected button की state manage करता है। ये हर radio button को track करता है कि कौन सा select हुआ है। जब कोई नया select होता है, तो बाकी buttons की selection state automatically false हो जाती है।

  • ButtonGroup खुद कोई UI component नहीं होता।
  • ये सिर्फ logical grouping के लिए use होता है।
  • इसका काम सिर्फ state management है, display का नहीं।

Dynamic Management in ButtonGroup

Dynamic management का मतलब है कि हम runtime में radio buttons को add या remove कर सकते हैं। Suppose आपके application में user preferences change हो रही हैं — तो आप dynamically group update कर सकते हैं।

Example: Adding Buttons Dynamically

JRadioButton option1 = new JRadioButton("Option 1"); JRadioButton option2 = new JRadioButton("Option 2"); ButtonGroup group = new ButtonGroup(); group.add(option1); group.add(option2); // अब runtime में नया button जोड़ते हैं JRadioButton option3 = new JRadioButton("Option 3"); group.add(option3);

इस तरह आप अपने program में dynamically नए radio buttons add कर सकते हैं और group की exclusivity maintain रह जाती है।

Removing Buttons Dynamically

ButtonGroup में direct remove() method नहीं होता, लेकिन आप logic से removal manage कर सकते हैं। जैसे कि किसी radio button को UI से remove कर दो और उसकी selection state clear कर दो।

group.clearSelection(); // सारे buttons deselect हो जाएंगे

Practical Example: Payment Mode Selection

अब एक practical example लेते हैं जहाँ user को payment mode चुनना है — UPI, Card या Cash।

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class PaymentModeExample { public static void main(String[] args) { JFrame frame = new JFrame("Payment Mode"); frame.setLayout(new FlowLayout()); JRadioButton upi = new JRadioButton("UPI"); JRadioButton card = new JRadioButton("Card"); JRadioButton cash = new JRadioButton("Cash"); ButtonGroup paymentGroup = new ButtonGroup(); paymentGroup.add(upi); paymentGroup.add(card); paymentGroup.add(cash); JButton check = new JButton("Check Payment Mode"); JLabel result = new JLabel(""); check.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (upi.isSelected()) result.setText("Selected Mode: UPI"); else if (card.isSelected()) result.setText("Selected Mode: Card"); else if (cash.isSelected()) result.setText("Selected Mode: Cash"); else result.setText("No mode selected"); } }); frame.add(upi); frame.add(card); frame.add(cash); frame.add(check); frame.add(result); frame.setSize(350, 200); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

इस example में जैसे ही user कोई payment option चुनता है और “Check Payment Mode” button दबाता है, selected mode label पर display हो जाता है। यही होता है Dynamic Management

Benefits of Using ButtonGroup

  • Single Selection Control: User confusion कम होती है।
  • Data Consistency: Form submission के समय सिर्फ एक valid option जाता है।
  • Cleaner UI: Multiple options logically grouped रहते हैं।
  • Dynamic Flexibility: Buttons runtime में manage किए जा सकते हैं।

Important Methods of ButtonGroup

Method Description
add(AbstractButton b) Radio button को group में जोड़ता है।
clearSelection() सभी buttons की selection clear करता है।
getSelection() वर्तमान में selected button को return करता है।
setSelected(ButtonModel m, boolean b) Programmatically किसी button को select करता है।

Programmatic Selection Example

कभी-कभी आपको manually किसी button को select करना पड़ता है, जैसे default option set करने के लिए।

ButtonGroup group = new ButtonGroup(); JRadioButton yes = new JRadioButton("Yes"); JRadioButton no = new JRadioButton("No"); group.add(yes); group.add(no); group.setSelected(yes.getModel(), true); // Yes button pre-selected रहेगा

Real-World Use Cases

  • Online Forms (Gender, Marital Status)
  • Exam Systems (Single Correct Answer)
  • Software Settings (Theme Selection)
  • Surveys & Feedback Systems

Key Points to Remember

  • ButtonGroup UI component नहीं होता — ये logical container है।
  • एक time में सिर्फ एक button select हो सकता है।
  • Dynamic add/remove possible है।
  • clearSelection() method group को reset करता है।
  • Programmatically भी selection control किया जा सकता है।

Notes (Exam-Oriented)

  • Definition: ButtonGroup Java Swing का class है जो radio buttons के बीच mutual exclusivity maintain करता है।
  • Package: javax.swing
  • Use: Multiple radio buttons को group करने के लिए ताकि एक बार में केवल एक select हो।
  • Important Method: add(), clearSelection(), getSelection(), setSelected()
  • Common Example: Gender, Payment Mode, Theme Selection
  • Mutual Exclusivity: इसका मतलब है — एक ही group में से सिर्फ एक button select हो सकता है।
  • Dynamic Management: Runtime में buttons को add या remove करना।