Feedback Form

Radio Button in Java: JRadioButton and ButtonGroup Guide

Radio Button in Java: JRadioButton and ButtonGroup Guide

अगर आप Java GUI (Graphical User Interface) सीख रहे हैं, तो Radio Button एक बहुत ही important component है। इसे हम तब use करते हैं जब user को एक group में से सिर्फ एक option select करने देना होता है। इस blog में हम detail में समझेंगे कि Radio Button in Java क्या है, JRadioButton कैसे काम करता है और ButtonGroup का क्या role होता है। यह article exam-oriented है और बिल्कुल आसान language में लिखा गया है ताकि कोई भी student इसे आराम से समझ सके।

What is Radio Button in Java?

Java में Radio Button एक ऐसा GUI component है जो multiple choices में से सिर्फ एक choice select करने की सुविधा देता है। उदाहरण के लिए, अगर हमें पूछना हो कि user का gender क्या है — Male, Female या Other — तो हम Radio Buttons का use करते हैं।

Radio Button को Java में implement करने के लिए JRadioButton class का use किया जाता है, जो javax.swing package के अंदर होती है।

JRadioButton Class Syntax

JRadioButton rb1 = new JRadioButton("Male"); JRadioButton rb2 = new JRadioButton("Female");

ऊपर के code में दो Radio Buttons बनाए गए हैं — एक Male के लिए और एक Female के लिए।

Why use JRadioButton?

JRadioButton का use तब किया जाता है जब हमें multiple options दिखाने हैं, लेकिन user को उनमें से सिर्फ एक select करने देना है। अगर आप चाहते हैं कि user multiple options select करे, तो उसके लिए JCheckBox का use किया जाता है।

  • Single selection behavior प्रदान करता है।
  • ButtonGroup के साथ मिलकर काम करता है।
  • Form या Survey में बहुत useful है।
  • Easy to implement and manage in GUI applications.

Constructors of JRadioButton

JRadioButton class के कई constructors होते हैं, जिनसे हम अलग-अलग तरीके से Radio Button बना सकते हैं। नीचे कुछ common constructors दिए गए हैं:

ConstructorDescription
JRadioButton()एक empty Radio Button बनाता है।
JRadioButton(String text)Text label के साथ Radio Button बनाता है।
JRadioButton(String text, boolean selected)Text के साथ Radio Button बनाता है और उसे selected या deselected set कर सकता है।
JRadioButton(Icon icon)Icon के साथ Radio Button बनाता है।

Common Methods of JRadioButton

JRadioButton के कुछ commonly used methods नीचे दिए गए हैं:

MethodDescription
setText(String text)Radio Button का text बदलने के लिए।
getText()Radio Button का text प्राप्त करने के लिए।
setSelected(boolean b)Button को selected या deselected set करने के लिए।
isSelected()Check करता है कि button selected है या नहीं।
setEnabled(boolean b)Radio Button को enable या disable करने के लिए।

Example of JRadioButton

अब देखते हैं एक simple example जिसमें तीन Radio Buttons हैं — Male, Female, Other — और इन्हें ButtonGroup में जोड़ा गया है ताकि एक बार में सिर्फ एक button select हो सके।

import javax.swing.*; import java.awt.*; import java.awt.event.*; class RadioExample { public static void main(String[] args) { JFrame frame = new JFrame("Radio Button Example"); JRadioButton rb1 = new JRadioButton("Male"); JRadioButton rb2 = new JRadioButton("Female"); JRadioButton rb3 = new JRadioButton("Other"); rb1.setBounds(100, 50, 100, 30); rb2.setBounds(100, 80, 100, 30); rb3.setBounds(100, 110, 100, 30); ButtonGroup bg = new ButtonGroup(); bg.add(rb1); bg.add(rb2); bg.add(rb3); JButton b = new JButton("Submit"); b.setBounds(100, 150, 100, 30); JLabel label = new JLabel("Select your gender"); label.setBounds(100, 20, 200, 30); frame.add(rb1); frame.add(rb2); frame.add(rb3); frame.add(b); frame.add(label); frame.setSize(300, 250); frame.setLayout(null); frame.setVisible(true); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String msg = ""; if (rb1.isSelected()) msg = "You selected Male."; else if (rb2.isSelected()) msg = "You selected Female."; else if (rb3.isSelected()) msg = "You selected Other."; else msg = "No selection."; JOptionPane.showMessageDialog(frame, msg); } }); } }

ऊपर के program में user जब कोई Radio Button select करके Submit पर click करता है, तो एक dialog box दिखता है जिसमें selection का result होता है।

ButtonGroup in Java

ButtonGroup एक ऐसा logical group होता है जिसमें multiple radio buttons को रखा जाता है ताकि एक समय में सिर्फ एक button select हो सके।

Example of ButtonGroup

अगर आप Radio Buttons को group में नहीं रखेंगे, तो user multiple buttons select कर सकता है। इसलिए ButtonGroup का use जरूरी है।

ButtonGroup bg = new ButtonGroup(); bg.add(rb1); bg.add(rb2); bg.add(rb3);

इस code से सभी buttons एक group में आ जाते हैं और अब user केवल एक option select कर सकता है।

Event Handling in Radio Button

Radio Button में event handling बहुत जरूरी होती है ताकि जब user कोई option select करे, तो कुछ action perform हो। इसके लिए हम ActionListener का use करते हैं।

Example of Event Handling

rb1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Male selected"); } });

इस तरह, जब भी user “Male” option select करेगा, console पर “Male selected” print होगा।

Advantages of JRadioButton

  • Simple and user-friendly component।
  • Single selection control देता है।
  • Easy to group using ButtonGroup।
  • GUI forms, quizzes, surveys में useful।

Difference Between JRadioButton and JCheckBox

BasisJRadioButtonJCheckBox
SelectionOnly one option select हो सकता है।Multiple options select हो सकते हैं।
GroupButtonGroup की जरूरत होती है।Grouping जरूरी नहीं।
Use CaseSingle choice questions।Multiple choice questions।
LookRound buttonSquare box

Real-life Use of Radio Button

  • Online forms में gender selection।
  • Survey या Feedback forms में options choose करना।
  • Settings panel में single preference select करना।

Exam-Oriented Notes

  • JRadioButton class javax.swing package में होती है।
  • यह single selection के लिए use होती है।
  • Multiple Radio Buttons को group करने के लिए ButtonGroup का use होता है।
  • ActionListener का use करके event handling की जाती है।
  • Important constructors: JRadioButton(String text), JRadioButton(String text, boolean selected)
  • Important methods: setSelected(), isSelected(), getText()
  • Difference between JRadioButton and JCheckBox जरूर याद रखें।
  • Exam में program-based questions पूछे जा सकते हैं।