35

I want to avoid an output like this:

Here is some text t
hat is being wrappe
d by characters, sp
litting words in ha
lf

I am using a JTextArea, with setLineWrap(true). How can I make it wrap words, though? (Is there a way to make it only wrap when there is a space or something?)

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Cherie
  • 365
  • 1
  • 3
  • 5

4 Answers4

61

JTextArea.setWrapStyleWord(true)?

EboMike
  • 76,846
  • 14
  • 164
  • 167
3

You could just go to the properties window of the text field and check the boxes for both wrap style word and line wrap. It works like a charm

the properties window

Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
Eliizabeth
  • 31
  • 1
2

If you want your text is wrapped by words and not by the characters ( in other words , if you want you half word also shifts to next line

Very simple just use wrapstyleword instead of linewrap Syntax - JTextArea.setwrapstyleword(true);

Note~ It can only be used when you linewrap is already true I hope it would help.

Source ~ My notebook

Credits - 49/51 { 49% to myself & 51% to my ip teachers Ms. Shalu }

~ AM 17

Anuj Mor
  • 21
  • 2
0

To make wrap by words like this:

JtextArea textArea = new JTextArea(8, 20);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);