36

I have a JTextArea in Java. When I place a large amount of text in it, the text area provides horizontal scrolling.

How can I make my text area wrap instead?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Carlo
  • 1,147
  • 3
  • 15
  • 23

6 Answers6

79

Use the JTextArea#setLineWrap method. This is also illustrated in the Swing JTextArea tutorial

Robin
  • 36,233
  • 5
  • 47
  • 99
  • 1
    That's a first. Most times I am too slow due to the creation of the links to the Javadoc which take some time, even if you know the API from the top of your head – Robin Jan 13 '12 at 23:29
  • 8
    You may also want to use [JTextArea#setWrapStyleWord](http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextArea.html#setWrapStyleWord(boolean)) if don't want it to break words. – Trevin Avery Jul 01 '14 at 18:57
31

Look at the API for the methods available to JTextArea, in particular setWrapStyleWord and setLineWrap.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
18

Try This :

jTextArea.setLineWrap(true);
Stéphane Bruckert
  • 21,706
  • 14
  • 92
  • 130
sathya
  • 1,404
  • 2
  • 15
  • 26
3

In a Swing GUI Designer Like Netbeans IDE,

you could just 'check' the lineWrap in jTextArea property window.

if property window is hidden:

Goto WINDOW -> IDE TOOLS -> Properties

or press

CTRL + SHIFT + 7

In Swing GUI

add the line

jTextArea.setLineWrap(true);
Kym NT
  • 670
  • 9
  • 28
0

you would need to use this to wrap text with multiple words.

JTextArea.setWrapStyleWord(true);
Jed
  • 1
  • 1
0

In the IntelliJ GUI Designer, you can just check the "lineWrap" box to get the wrapping functionality: enter image description here

Code on the Rocks
  • 11,488
  • 3
  • 53
  • 61