This is a tutorial on how to use the
JTextArea Component of Java. JTextArea is different from JTextField. Unlike JTextField that will only be used for one line of text, the JTextArea Component can hold more than one line to display text.
So, now let's start this tutorial!
1. Open JCreator or NetBeans and make a java program with a file name of
jTextArea.java.
2. Import java.awt.* and javax.swing.* packages because we will going to have the JTextArea component and also the JPanel as the container of this.
import java.awt.*;
import javax.swing.*;
3. Instantiate a variable for JTextArea component.
The
5 here means the rows and
18 means the columns of the JTextArea.
4. Create a constructor that is the same with the filename and set textarea's initial text, scrollbar, and border.
public jTextArea() {
txtArea.setText("Encode more text to see scrollbars");
Get the content pane, set layout, add to center.
Set window characteristics of the program.
this.setContentPane(content);
this.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE); this.pack();
5. In your Main, create a JFrame component that will hold the panel and the textArea and all other components. This will set the size, location, title, and visibility.
public static void main
(String[] args
) { JFrame txtArea
= new jTextArea
(); txtArea.setTitle("JTextArea Component");
txtArea.setVisible(true);
txtArea.setSize(250,140);
txtArea.setLocation(300,300);
}
Output:
Here's the full code of this tutorial:
import java.awt.*;
import javax.swing.*;
public class jTextArea
extends JFrame {
public jTextArea() {
txtArea.setText("Encode more text to see scrollbars");
this.setContentPane(content);
this.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE); this.pack();
}
public static void main
(String[] args
) { JFrame txtArea
= new jTextArea
(); txtArea.setTitle("JTextArea Component");
txtArea.setVisible(true);
txtArea.setSize(250,140);
txtArea.setLocation(300,300);
}
}
Hope this helps! :)
Best Regards,
Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:
[email protected]
Add and Follow me on Facebook:
https://www.facebook.com/donzzsky
Visit and like my page on Facebook at:
https://www.facebook.com/BermzISware