Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap text in a JTextArea

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?

like image 793
Carlo Avatar asked Jan 13 '12 23:01

Carlo


People also ask

How do you wrap text in Java?

The first wrap() method takes the String to wrap as its first argument and the wrap length as its second argument. The second wrap() method takes for arguments. The first is the String to wrap and the second is the wrap length. The third is the newline characters to use when a line is wrapped.

How do I add text to JTextArea?

Use textArea. append("text") , but I recomend JTextPane for more control like color, selection, etc.

How do you wrap text in Jlabel?

As per @darren's answer, you simply need to wrap the string with <html> and </html> tags: myLabel. setText("<html>"+ myString +"</html>"); You do not need to hard-code any break tags.


1 Answers

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

like image 90
Robin Avatar answered Sep 19 '22 04:09

Robin