JTextArea: Multi-Line Text, Scrolling, and Word Wrap Configuration
JTextArea: Multi-Line Text, Scrolling, and Word Wrap Configuration
Introduction
Java में जब हम Graphical User Interface (GUI) बनाते हैं, तो हमें कई बार ऐसे components की जरूरत होती है जो multi-line text input को support करें। ऐसे में JTextArea सबसे useful component होता है। यह javax.swing package का हिस्सा है और इसे mainly multi-line text दिखाने या edit करने के लिए use किया जाता है।
Exam के लिए अगर पूछा जाए कि — "What is JTextArea in Java?" तो simple answer होगा — JTextArea एक text component है जो multiple lines में text को display और edit करने की सुविधा देता है।
Features of JTextArea
- Multi-line text input और display की सुविधा।
- Scrollable area add करने के लिए JScrollPane के साथ use किया जा सकता है।
- Word wrap और line wrap options available होते हैं।
- Programmatically text को set या append किया जा सकता है।
- Font, background, और foreground customize की जा सकती है।
JTextArea Constructor
JTextArea class के कई constructors होते हैं, जो अलग-अलग situations के लिए use किए जाते हैं। नीचे कुछ common constructors दिए गए हैं:
| Constructor | Description |
|---|---|
JTextArea() |
एक empty text area बनाता है। |
JTextArea(String text) |
Text area बनाता है और उसे दिए गए text से initialize करता है। |
JTextArea(int rows, int columns) |
Text area को rows और columns के साथ बनाता है। |
JTextArea(String text, int rows, int columns) |
दिए गए text, rows, और columns के साथ text area बनाता है। |
Basic Example of JTextArea
अब एक simple example देखते हैं जहाँ हम JTextArea create करेंगे और उसे frame में display करेंगे।
import javax.swing.*;
public class TextAreaExample {
public static void main(String args[]) {
JFrame frame = new JFrame("JTextArea Example");
JTextArea area = new JTextArea("Welcome to JTextArea Example");
area.setBounds(10, 30, 300, 200);
frame.add(area);
frame.setSize(400, 400);
frame.setLayout(null);
frame.setVisible(true);
}
}
ऊपर के example में हमने एक simple frame बनाया और उसमें JTextArea को add किया है। अब इस पर text को freely edit किया जा सकता है।
Multi-Line Text Input in JTextArea
JTextArea default रूप से multi-line input support करता है। इसका मतलब है कि आप Enter key दबाकर नई line शुरू कर सकते हैं। किसी भी text box में multiple lines लेने के लिए JTextArea सबसे perfect choice होती है।
Adding Scrolling Feature
अगर आपका text बहुत बड़ा है और JTextArea के size से बाहर चला जाता है, तो हमें scroll bars की जरूरत पड़ती है। इसके लिए हम JScrollPane का use करते हैं। JScrollPane automatically vertical और horizontal scroll bars provide करता है।
import javax.swing.*;
public class ScrollableTextAreaExample {
public static void main(String args[]) {
JFrame frame = new JFrame("Scrollable JTextArea Example");
JTextArea area = new JTextArea(10, 30);
area.setText("यह एक multi-line JTextArea example है...\nयह line wrapping और scrolling दिखाता है।");
JScrollPane scrollPane = new JScrollPane(area);
frame.add(scrollPane);
frame.setSize(400, 300);
frame.setVisible(true);
}
}
ऊपर के program में हमने JTextArea को JScrollPane के अंदर रखा है ताकि scroll bars automatically appear हों जब content बड़ा हो जाए।
Word Wrap and Line Wrap Configuration
Word wrap feature बहुत useful होता है क्योंकि यह text को automatically अगली line में ले जाता है जब current line frame की width से बड़ी हो जाती है। JTextArea में दो main wrapping methods होती हैं:
- setLineWrap(true) — यह line wrapping enable करता है।
- setWrapStyleWord(true) — यह words को proper तरीके से break करता है (word के बीच में नहीं)।
import javax.swing.*;
public class WordWrapExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Word Wrap Example");
JTextArea area = new JTextArea(10, 30);
area.setLineWrap(true);
area.setWrapStyleWord(true);
area.setText("यह example दिखाता है कि JTextArea में word wrap कैसे enable किया जाता है। जब text लंबा हो जाता है, तो यह अपने आप अगली line में चला जाता है।");
JScrollPane scroll = new JScrollPane(area);
frame.add(scroll);
frame.setSize(400, 300);
frame.setVisible(true);
}
}
इस example में line wrapping और word wrapping दोनों enable हैं, जिससे text हमेशा readable और neat दिखता है।
Commonly Used Methods of JTextArea
| Method | Description |
|---|---|
setText(String text) |
JTextArea में नया text set करता है। |
getText() |
Current text को return करता है। |
append(String text) |
Existing text के बाद नया text add करता है। |
setLineWrap(boolean wrap) |
Line wrapping enable या disable करता है। |
setWrapStyleWord(boolean word) |
Word wrapping enable या disable करता है। |
setEditable(boolean editable) |
Text editing को enable या disable करता है। |
Editable और Non-Editable JTextArea
कई बार हमें सिर्फ text display करना होता है, edit नहीं करना होता। ऐसे में हम setEditable(false) का use करते हैं।
JTextArea area = new JTextArea("यह text सिर्फ display के लिए है।");
area.setEditable(false);
अब user इस text को देख तो सकता है, लेकिन उसमें कोई बदलाव नहीं कर सकता।
JTextArea vs JTextField
JTextArea और JTextField दोनों text components हैं, लेकिन इनके uses अलग-अलग होते हैं। नीचे difference table दी गई है:
| Feature | JTextField | JTextArea |
|---|---|---|
| Lines | Single-line input | Multi-line input |
| Scrolling | Not supported | Supported with JScrollPane |
| Word Wrap | Not available | Available |
| Use Case | Name, Email input | Comments, Description input |
Practical Example: Feedback Form using JTextArea
अब एक छोटा practical example देखते हैं जहाँ JTextArea का use feedback form में किया गया है।
import javax.swing.*;
import java.awt.event.*;
public class FeedbackForm {
public static void main(String[] args) {
JFrame frame = new JFrame("Feedback Form");
JLabel label = new JLabel("Your Feedback:");
label.setBounds(20, 20, 100, 30);
JTextArea area = new JTextArea(10, 30);
area.setLineWrap(true);
area.setWrapStyleWord(true);
JScrollPane scroll = new JScrollPane(area);
scroll.setBounds(20, 60, 300, 150);
JButton submit = new JButton("Submit");
submit.setBounds(120, 230, 100, 30);
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String feedback = area.getText();
JOptionPane.showMessageDialog(frame, "Thanks for your feedback!");
}
});
frame.add(label);
frame.add(scroll);
frame.add(submit);
frame.setSize(360, 320);
frame.setLayout(null);
frame.setVisible(true);
}
}
यह example दिखाता है कि कैसे JTextArea को real-world form में इस्तेमाल किया जा सकता है। यह user से multi-line feedback लेता है और submit button पर response दिखाता है।
Exam-Oriented Notes (JTextArea Important Points)
- JTextArea multi-line text input और display के लिए use होता है।
- यह javax.swing package का हिस्सा है।
- Scrollable area add करने के लिए JScrollPane use किया जाता है।
- Line wrapping enable करने के लिए
setLineWrap(true)method use करें। - Word wrapping enable करने के लिए
setWrapStyleWord(true)method use करें। - Editable को false करने से text read-only बन जाता है।
- Commonly used methods —
setText(),getText(),append()। - JTextArea को layout में place करने के लिए scroll pane का use best practice माना जाता है।
- Exam में अक्सर पूछा जाता है: "Differentiate between JTextArea and JTextField" — तो याद रखें कि JTextField single-line होता है जबकि JTextArea multi-line।