Feedback Form

Essential Swing Components: JButton, JLabel, JTextField, JCheckBox, JRadioButton

Essential Swing Components in Java: JButton, JLabel, JTextField, JCheckBox, JRadioButton

Introduction to Swing Components

Java में GUI (Graphical User Interface) बनाने के लिए Swing एक बहुत ही popular और powerful toolkit है। Swing, AWT (Abstract Window Toolkit) का advanced version है, जो ज्यादा flexible और lightweight components provide करता है। इन components की मदद से हम interactive desktop applications बना सकते हैं। आज हम बात करेंगे कुछ essential Swing components जैसे — JButton, JLabel, JTextField, JCheckBox, और JRadioButton के बारे में।

What is Swing in Java?

Swing एक part है Java Foundation Classes (JFC) का, जिसे Java द्वारा GUI बनाने के लिए develop किया गया था। Swing components lightweight होते हैं क्योंकि ये operating system के native code पर depend नहीं करते, बल्कि pure Java में लिखे गए हैं। इसलिए ये हर platform पर same look and feel provide करते हैं — इसे हम platform-independent GUI toolkit कहते हैं।

Features of Swing

  • Lightweight और platform-independent components
  • Pluggable Look and Feel support
  • MVC (Model-View-Controller) architecture पर आधारित
  • Rich set of GUI components जैसे JButton, JLabel, JTable, JTextField आदि
  • Event-driven programming support

JButton in Swing

JButton एक interactive button component है जो किसी action को perform करने के लिए use किया जाता है। जब user इस पर click करता है, तो एक event generate होता है जिसे program handle करता है। उदाहरण के लिए “Submit”, “Login”, “OK”, “Cancel” जैसे buttons को JButton के जरिए बनाया जाता है।

Creating a JButton

JButton btn = new JButton("Click Me");

Adding JButton to Frame

JFrame frame = new JFrame("Button Example");
JButton button = new JButton("Submit");
frame.add(button);
frame.setSize(300,200);
frame.setLayout(new FlowLayout());
frame.setVisible(true);

JButton Key Points

  • Button पर click होने पर ActionEvent generate होता है।
  • हम ActionListener interface का use करके event handle करते हैं।
  • Button के text और icon दोनों set किए जा सकते हैं।

JLabel in Swing

JLabel का use किसी text या image को display करने के लिए किया जाता है। यह user से input नहीं लेता, सिर्फ information दिखाने के लिए होता है।

Creating a JLabel

JLabel label = new JLabel("Welcome to Java Swing!");

JLabel Example

JFrame f = new JFrame("Label Example");
JLabel label = new JLabel("Enter your name:");
f.add(label);
f.setSize(300,150);
f.setLayout(new FlowLayout());
f.setVisible(true);

JLabel Key Points

  • Text के साथ image भी दिखा सकते हैं।
  • setText() और setIcon() methods से content बदला जा सकता है।
  • Alignment (LEFT, RIGHT, CENTER) भी set किया जा सकता है।

JTextField in Swing

JTextField एक single-line text input field है जिसका उपयोग user से data लेने के लिए किया जाता है। जैसे किसी login form में username या email address enter करने के लिए।

Creating a JTextField

JTextField textField = new JTextField(20);

Example of JTextField

JFrame f = new JFrame("TextField Example");
JTextField tf = new JTextField("Type here...", 20);
f.add(tf);
f.setLayout(new FlowLayout());
f.setSize(300,200);
f.setVisible(true);

JTextField Key Points

  • User से text input लेने के लिए उपयोग किया जाता है।
  • setText() और getText() methods से value read या set कर सकते हैं।
  • हम password जैसी sensitive जानकारी के लिए JPasswordField का भी उपयोग करते हैं।

JCheckBox in Swing

JCheckBox का उपयोग user को multiple options select करने की सुविधा देने के लिए किया जाता है। हर checkbox independent होता है — यानी user एक या एक से ज्यादा checkbox select कर सकता है।

Creating JCheckBox

JCheckBox cb1 = new JCheckBox("Java");
JCheckBox cb2 = new JCheckBox("Python");
JCheckBox cb3 = new JCheckBox("C++");

JCheckBox Example

JFrame f = new JFrame("CheckBox Example");
JCheckBox cb1 = new JCheckBox("Java", true);
JCheckBox cb2 = new JCheckBox("Python");
f.add(cb1);
f.add(cb2);
f.setLayout(new FlowLayout());
f.setSize(300,200);
f.setVisible(true);

JCheckBox Key Points

  • Multiple selection possible होती है।
  • isSelected() method से check कर सकते हैं कि checkbox select है या नहीं।
  • Form या survey जैसी situations में use किया जाता है।

JRadioButton in Swing

JRadioButton का उपयोग तब किया जाता है जब हमें user से किसी group में से केवल एक option select करवाना हो। उदाहरण के लिए — Gender selection (“Male”, “Female”, “Other”)।

Creating JRadioButton

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

Group Radio Buttons

Radio buttons को एक group में combine करने के लिए ButtonGroup class का उपयोग किया जाता है।

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

JRadioButton Example

JFrame f = new JFrame("RadioButton Example");
JRadioButton rb1 = new JRadioButton("Male");
JRadioButton rb2 = new JRadioButton("Female");
ButtonGroup bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
f.add(rb1);
f.add(rb2);
f.setLayout(new FlowLayout());
f.setSize(300,200);
f.setVisible(true);

JRadioButton Key Points

  • एक group में से केवल एक option select किया जा सकता है।
  • ButtonGroup class selection control करती है।
  • Option forms या setting menus में commonly use होता है।

Difference Between JCheckBox and JRadioButton

Basis JCheckBox JRadioButton
Selection Type Multiple options select किए जा सकते हैं। केवल एक option select किया जा सकता है।
Independence हर checkbox independent होता है। Radio buttons एक group में जुड़े होते हैं।
Common Use Multiple answer forms, preferences Single choice questions जैसे Gender selection

Summary of Swing Components

नीचे एक quick summary दी गई है ताकि आप exam या practical में तुरंत याद रख सकें —

Component Purpose Example
JButton Action perform करने के लिए Submit, OK, Cancel
JLabel Text या Image दिखाने के लिए "Enter Name:"
JTextField User से text input लेने के लिए Username, Email
JCheckBox Multiple selection options के लिए Java, Python, C++
JRadioButton Single selection options के लिए Male, Female

Exam-Oriented Notes

  • Swing lightweight और platform-independent GUI toolkit है।
  • JButton किसी action को trigger करने के लिए use होता है।
  • JLabel केवल information display करता है।
  • JTextField single-line text input field होता है।
  • JCheckBox multiple selection की अनुमति देता है।
  • JRadioButton group में single selection enforce करता है।
  • ButtonGroup का use radio buttons को group करने के लिए होता है।
  • Event handling के लिए ActionListener और ItemListener commonly use किए जाते हैं।
  • All Swing components javax.swing पैकेज में defined हैं।
  • Frame बनाने के लिए JFrame सबसे important container class है।