• We just launched and are currently in beta. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

JTextArea Component in Java GUI

alcapone91

User Engagement Optimizer
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
131
Likes
107
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
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:

jtextarea.png


Here's the full code of this tutorial:

  1. import

    java.awt.*

    ;
  2. import

    javax.swing.*

    ;

  3. public

    class

    jTextArea extends

    JFrame

    {

  4. JTextArea

    txtArea =

    new

    JTextArea

    (

    5

    ,18

    )

    ;

  5. public

    jTextArea(

    )

    {

  6. txtArea.setText

    (

    "Encode more text to see scrollbars"

    )

    ;
  7. JScrollPane

    scrollingArea =

    new

    JScrollPane

    (

    txtArea)

    ;

  8. JPanel

    content =

    new

    JPanel

    (

    )

    ;
  9. content.setLayout

    (

    new

    BorderLayout

    (

    )

    )

    ;
  10. content.add

    (

    scrollingArea, BorderLayout

    .CENTER

    )

    ;

  11. this

    .setContentPane

    (

    content)

    ;
  12. this

    .setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;
  13. this

    .pack

    (

    )

    ;
  14. }

  15. public

    static

    void

    main(

    String

    [

    ]

    args)

    {
  16. JFrame

    txtArea =

    new

    jTextArea(

    )

    ;
  17. txtArea.setTitle

    (

    "JTextArea Component"

    )

    ;
  18. txtArea.setVisible

    (

    true

    )

    ;
  19. txtArea.setSize

    (

    250

    ,140

    )

    ;
  20. txtArea.setLocation

    (

    300

    ,300

    )

    ;
  21. }
  22. }

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


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

343,183

343,191

Top