In this section we will discuss about the javax.swing.JTextArea.
javax.swing.JTextArea extends the JTextComponent. JTextArea facilitates to display plain text in multiple lines. This is a lightweight component rather than the java.awt.TextArea. This component has more capabilities rather than the java.awt.TextArea. As compare to java.awt.TextArea, javax.swing.JTextArea doesn't manage the scrolling itself. To manage scrolling the swing interface Scrollable is implemented by this class due to this interface this class is capable to be placed within the JScrollpane if JtextArea is required to add scrolling behavior otherwise it can be used without JScrollpane.
Syntax :
public class JTextArea extends JTextComponent
Constructor Detail
| JTextArea() | A default constructor creates a new textarea. Syntax : public JTextArea() |
| JTextArea(Document doc) | This constructor creates a new JTextArea object with the specified document
model. Syntax : public JTextArea(Document doc) |
| JTextArea(Document doc, String text, int rows, int columns) | This constructor creates a new JTextArea object with the specified document
model, string, rows and columns. Syntax : public JTextArea(Document doc, String text, int rows, int columns) |
| JTextArea(int rows, int columns) | This constructor creates a new JTextArea object with the given number of
rows and columns. Syntax : public JTextArea(int rows, int columns) |
| JTextArea(String text) | This constructor creates a new JTextArea object with the given string. Syntax : public JTextArea(String text) |
| JTextArea(String text, int rows, int columns) | This constructor creates a new JTextArea object with the given string and
the given number of rows and columns. Syntax : public JTextArea(String text, int rows, int columns) |
Method Detail
Some commonly used methods of this class are as follows :
Mostly used methods inherited from javax.swing.text.JTextComponent class
Description of the methods given below is in perspective to the JTextArea.
Except the above methods there are various of methods that are inherited from its parent class.
Example
Here I am going to give a simple example of JTextArea which will demonstrate you about how the JTextArea can be used in the programming. In this example I have created a Java class named JavaJTextAreaExample.java. In this class I have created four different textarea. Different textarea in the sense textarea with different features.
Source Code
JavaJTextAreaExample.java
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import java.awt.Font;
public class JavaJTextAreaExample
{
private final static String newline = "\n";
JTextArea tarea1= new JTextArea();// creates a default textarea.
JTextArea tarea2 = new JTextArea(10,20); // creates textarea with three rows and two columns.
JTextArea tarea3 = new JTextArea("Java JTextArea Example");// creates textarea with the specified text.
JTextArea tarea4 = new JTextArea("Java JTextArea Example", 3, 2);//creates textarea with the specified
text and with the three rows and two columns.
JFrame frame;
public void createUI()
{
frame = new JFrame("JTextArea Example");
frame.setLayout(null);
frame.setSize(700,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lab1 = new JLabel("Default TextArea");
JLabel lab2 = new JLabel("TextArea with 3 rows and 2 columns");
JLabel lab3 = new JLabel("TextArea with specified text");
JLabel lab4 = new JLabel("TextArea with 3 rows and 2 columns with the specified text");
tarea1.setText("Text set by the user");
tarea1.append(newline+"NEW TEXT");
lab1.setBounds(10,40,300,20);
tarea1.setBounds(370,40,150,80);
tarea2.setFont(new Font("SERIF", Font.BOLD, 30));
lab2.setBounds(10,130,300,20);
tarea2.setBounds(370,130,150,80);
tarea3.setEditable(false);
lab3.setBounds(10,210,300,20);
tarea3.setBounds(370,220,150,80);
tarea4.setEditable(true);
lab4.setBounds(10,300,350,20);
tarea4.setBounds(370,310,150,80);
frame.add(lab1);
frame.add(tarea1);
frame.add(lab2);
frame.add(tarea2);
frame.add(lab3);
frame.add(tarea3);
frame.add(lab4);
frame.add(tarea4);
}// createUI() closed
public static void main(String args[])
{
JavaJTextAreaExample jtae = new JavaJTextAreaExample();
jtae.createUI();
}// main() closed
}// class closed
Output
When you will execute the above example you will get the output as follows :

In the above image first textarea contains text set by setText() method and the text "NEW TEXT " is written into the new line using append() method, in the second textarea I have used the setFont() method and write the text, in the third textarea make it non editable using setEditable(false) method, and makes the fourth textarea editable using setEditable(true) method.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Java JTextArea
Post your Comment