Search code examples
javaswingjscrollpanejtextareaword-wrap

Text goes outside JTextArea


I am trying to do something in Java which requires me to have a JTextArea in a ScrollPane.

Here is how I have defined them:

private JTextArea longestparagraph = new JTextArea();

....
JScrollPane scrollpanedreapta = new JScrollPane(longestparagraph, 
                        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
                        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollpanedreapta.setBorder(BorderFactory.createTitledBorder("Cel mai lung paragraf:"));

The problem I'm encountering is that the text doesn't start on a new line when it reaches the right border of the TextArea but it continues. Got any ideas how to solve that? Here's a picture to make my statement a little bit clearer.

enter image description here


Solution

  • Found the answer. Just had to this:

    longestparagraph.setLineWrap(true);
    longestparagraph.setWrapStyleWord(true);