Feedback Form

Text Boxes in Java (AWT & Swing): JTextField, JTextArea, JPasswordField

Text Boxes in Java (AWT & Swing): JTextField, JTextArea, JPasswordField

जब हम Java GUI (Graphical User Interface) बनाते हैं, तो हमें User से Text input लेने की ज़रूरत पड़ती है। इस काम के लिए Java AWT और Swing दोनों में Text Box का इस्तेमाल किया जाता है। Text Boxes को Java में JTextField, JTextArea और JPasswordField के रूप में उपयोग किया जाता है।

इस ब्लॉग में हम Step-by-step समझेंगे कि ये Text Boxes कैसे काम करते हैं, कैसे बनाए जाते हैं, और उनके बीच क्या फर्क है — साथ ही Example Program भी देखेंगे ताकि Exam और Practicals दोनों के लिए ये Topic पूरी तरह clear हो जाए।

What is Text Box in Java?

Text Box एक ऐसा GUI Component है जो User को Text Enter करने की सुविधा देता है। Java में Text Boxes AWT (Abstract Window Toolkit) और Swing दोनों Libraries में available हैं।

  • AWT: पुराने version का toolkit है जो system-dependent components पर काम करता है।
  • Swing: आधुनिक toolkit है जो lightweight और platform-independent components देता है।

Text Boxes की मदद से हम User से Input लेकर उसे Process कर सकते हैं — जैसे किसी Form में Name, Password या Message लेना।

Types of Text Boxes in Java

Java में Text Boxes तीन मुख्य प्रकार के होते हैं:

  • JTextField: Single line text input के लिए।
  • JTextArea: Multi-line text input के लिए।
  • JPasswordField: Password input के लिए जहाँ entered text hidden रहता है।

JTextField in Java

JTextField Swing Class का हिस्सा है जिसका इस्तेमाल Single-line text input के लिए किया जाता है। यह Text Enter करने, Edit करने और Programmatically Access करने के काम आता है।

Constructor of JTextField

  • JTextField() — empty text field बनाता है।
  • JTextField(int columns) — specific width (columns) वाला text field बनाता है।
  • JTextField(String text) — text के साथ pre-filled text field बनाता है।
  • JTextField(String text, int columns) — text और width दोनों specify करता है।

Common Methods of JTextField

Method Description
getText() TextField का current text return करता है।
setText(String t) TextField में नया text set करता है।
setEditable(boolean b) TextField को editable या non-editable बनाता है।
setColumns(int n) TextField की width set करता है।

Example of JTextField

import javax.swing.*;
class TextFieldExample {
  public static void main(String args[]) {
    JFrame f = new JFrame("JTextField Example");
    JTextField t1 = new JTextField("Welcome to Java");
    t1.setBounds(50, 100, 200, 30);
    f.add(t1);
    f.setSize(400, 300);
    f.setLayout(null);
    f.setVisible(true);
  }
}

ऊपर के Example में एक JFrame बनाया गया है जिसमें एक Text Field लगाया गया है। इस Text Field में “Welcome to Java” pre-filled text दिखेगा।

JTextArea in Java

JTextArea Multi-line text input के लिए उपयोग किया जाता है। अगर User को paragraph या बड़ा text enter करना है, तो JTextArea best option है।

Constructor of JTextArea

  • JTextArea() — Empty text area बनाता है।
  • JTextArea(String text) — दिए गए text से भरा text area बनाता है।
  • JTextArea(int rows, int columns) — specific size वाला text area बनाता है।
  • JTextArea(String text, int rows, int columns) — text और size दोनों define करता है।

Common Methods of JTextArea

Method Description
append(String text) Existing text के बाद नया text जोड़ता है।
getText() TextArea का current text return करता है।
setText(String t) TextArea में नया text set करता है।
setLineWrap(boolean wrap) Line wrapping enable/disable करता है।
setWrapStyleWord(boolean word) Word boundary पर wrapping enable करता है।

Example of JTextArea

import javax.swing.*;
class TextAreaExample {
  public static void main(String args[]) {
    JFrame f = new JFrame("JTextArea Example");
    JTextArea area = new JTextArea("This is a multi-line text area.");
    area.setBounds(50, 100, 300, 200);
    area.setLineWrap(true);
    f.add(area);
    f.setSize(400, 400);
    f.setLayout(null);
    f.setVisible(true);
  }
}

इस example में User multiple lines में text enter कर सकता है, और text automatically wrap हो जाएगा ताकि readability बेहतर बनी रहे।

JPasswordField in Java

JPasswordField का इस्तेमाल Password लेने के लिए किया जाता है। इसमें entered characters दिखाई नहीं देते — उनकी जगह asterisks (*) या dots (•) दिखते हैं ताकि password secure रहे।

Constructor of JPasswordField

  • JPasswordField() — empty password field बनाता है।
  • JPasswordField(int columns) — specific width वाला password field बनाता है।
  • JPasswordField(String text) — text के साथ pre-filled password field बनाता है।

Common Methods of JPasswordField

Method Description
getPassword() Password को character array के रूप में return करता है।
setEchoChar(char c) Character को set करता है जो password typing के समय दिखेगा।
setText(String t) Predefined text set करता है।

Example of JPasswordField

import javax.swing.*;
class PasswordFieldExample {
  public static void main(String args[]) {
    JFrame f = new JFrame("JPasswordField Example");
    JPasswordField pass = new JPasswordField();
    pass.setBounds(100, 100, 150, 30);
    pass.setEchoChar('*');
    f.add(pass);
    f.setSize(400, 300);
    f.setLayout(null);
    f.setVisible(true);
  }
}

ऊपर के example में User जब password type करेगा, तो screen पर stars (*) दिखाई देंगे, जिससे input secure रहेगा।

Difference between JTextField, JTextArea, and JPasswordField

Feature JTextField JTextArea JPasswordField
Input Type Single Line Multi Line Hidden Input (Password)
Class Swing Component Swing Component Swing Component
Visibility Text Visible Text Visible Text Hidden
Usage Names, Single Words Comments, Paragraphs Password Entry

AWT vs Swing Text Boxes

Java में AWT और Swing दोनों में Text Boxes available हैं, लेकिन Swing versions ज्यादा advanced और flexible होते हैं।

AWT Component Swing Equivalent
TextField JTextField
TextArea JTextArea

AWT Text Boxes system-dependent होते हैं जबकि Swing Text Boxes platform-independent होते हैं और look & feel भी modern होता है।

Exam-Oriented Notes on Text Boxes in Java

  • Text Box: User input लेने वाला component।
  • JTextField: Single line text entry के लिए।
  • JTextArea: Multi line text entry के लिए।
  • JPasswordField: Hidden password input के लिए।
  • AWT vs Swing: Swing अधिक flexible और modern होता है।
  • getText() — text प्राप्त करने के लिए।
  • setText() — नया text set करने के लिए।
  • append() — JTextArea में नया text जोड़ने के लिए।
  • setEchoChar() — JPasswordField में character set करने के लिए।
  • Text Boxes Java GUI development का सबसे basic और exam-important concept है।