Advertisement



< Prev
Next >



JTextArea class




JTextArea class is used to create a multi-line text area, allowing a user to enter the text in multiple lines. JTextArea is a lightweight component which extends TextComponent class, which further extends JComponent class. To read the text entered in JTextArea, one has to implement ActionListerner interface.




Constructors of JTextArea


Constructor Description
public JTextArea() Creates a new TextArea.
public JTextArea(String text) Creates a new TextArea with specified text.
public JTextArea(int rows, int columns) Creates a new TextArea with specified number of rows and columns..
public JTextArea(String text, int rows, int columns) Creates a new TextArea with a text and a number of rows and columns.





Methods of JTextArea class


Methods Description
public void setText(String text) Sets a String message on the JTextArea.
public String getText() Gets a String message of JTextArea.
public void append(String text) Appends the text to the JTextArea.
public int getRows() Gets the total number of rows in JTextArea.
public int getColumns() Gets the total number of columns in JTextArea.





An example of JTextArea


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;



public class TextAr1
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class TextAr1


class A //implements ActionListener
{
JFrame jf;

JTextArea textArea1, textArea2, textArea3, textArea4;

A()
{
jf= new JFrame("JTextArea");




textArea1 = new JTextArea();		//JTextArea()
textArea2 = new JTextArea(2,2);		//JTextArea(int rows, int columns)
textArea3 = new JTextArea("Third textarea", 4,4);
textArea4 = new JTextArea("Fourth textarea");



textArea1.append("First textarea");
textArea2.append("Second textarea");



jf.add(textArea1);
jf.add(textArea2);
jf.add(textArea3);
jf.add(textArea4);

jf.setLayout(new FlowLayout());
jf.setSize(400,200);
jf.setVisible(true);
}

}

When you run the code, you are presented a window shown below -:

Figure 1



Advertisement




Reading the data entered in JTextArea by implementing ActionListener


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TextAr2
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class TextAr1


class A implements ActionListener
{
JFrame jf;
JTextArea textArea1;
JLabel label1, label2; 
JButton button;


A()
{
jf= new JFrame("JTextArea");
label1 = new JLabel("Please mention your hobbies in the textbox");
button = new JButton("Submit");
label2 = new JLabel();


textArea1 = new JTextArea(5,32);

jf.add(label1);
jf.add(textArea1);
jf.add(button);
jf.add(label2);

button.addActionListener(this);

jf.setLayout(new FlowLayout());
jf.setSize(380,250);
jf.setVisible(true);
}


public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("Submit"))
{
label2.setText(textArea1.getText());
}
}


}
When you run the code, you are presented a window shown in the Figure2. In this window you are asked to enter your hobbies in the textbox -:

Figure 2


Once you are done with entering your hobbies in the textbox and the moment you click on Submit button, all the data(your hobbies) entered in JTextArea is read and displayed in the window.

Figure3




Please share this article -





< Prev
Next >
< JTextField
JButton >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement