JTextArea Component in Java GUI

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.
  1. import java.awt.*;
  2. import javax.swing.*;
3. Instantiate a variable for JTextArea component.
  1. JTextArea txtArea = new JTextArea(5,18);
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.
  1. public jTextArea() {
  2. txtArea.setText("Encode more text to see scrollbars");
  3. JScrollPane scrollingArea = new JScrollPane(txtArea);
Get the content pane, set layout, add to center.
  1. JPanel content = new JPanel();
  2. content.setLayout(new BorderLayout());
  3. content.add(scrollingArea, BorderLayout.CENTER);
Set window characteristics of the program.
  1. this.setContentPane(content);
  2. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  3. 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.
  1. public static void main(String[] args) {
  2. JFrame txtArea = new jTextArea();
  3. txtArea.setTitle("JTextArea Component");
  4. txtArea.setVisible(true);
  5. txtArea.setSize(250,140);
  6. txtArea.setLocation(300,300);
  7. }
Output: output Here's the full code of this tutorial:
  1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4. public class jTextArea extends JFrame {
  5.  
  6. JTextArea txtArea = new JTextArea(5,18);
  7.  
  8. public jTextArea() {
  9.  
  10. txtArea.setText("Encode more text to see scrollbars");
  11. JScrollPane scrollingArea = new JScrollPane(txtArea);
  12.  
  13. JPanel content = new JPanel();
  14. content.setLayout(new BorderLayout());
  15. content.add(scrollingArea, BorderLayout.CENTER);
  16.  
  17. this.setContentPane(content);
  18. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19. this.pack();
  20. }
  21.  
  22. public static void main(String[] args) {
  23. JFrame txtArea = new jTextArea();
  24. txtArea.setTitle("JTextArea Component");
  25. txtArea.setVisible(true);
  26. txtArea.setSize(250,140);
  27. txtArea.setLocation(300,300);
  28. }
  29. }
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

Add new comment