Reputation: 215
I want to add text to a JTextArea, and to have an auto scrollbar on vertical.
but when typing horizonally, I want an auto new line when there is no space in line.. If I use only JTextArea it's OK, but when I put it in a JScrollPane, it is not make a new line when needed.
How can I do that?
Thanks!
Upvotes: 0
Views: 2819
Reputation: 6526
By default, a JTextArea
will not wrap text so you have to manually define that behavior:
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
Also, make sure you're setting up the JScrollPane
correctly:
JScrollPane sp = new JScrollPane(textArea);
//JScrollPanes are just like JPanels (except for the scrollbars) so be careful not to just add the JComponent to your frame; add the container instead.
frame.add(sp);
As a side note, read the tutorial @kleopatra so helpfully suggested to get a good solid base on textareas.
Upvotes: 0
Reputation: 51536
you have to configure the textArea to wrap:
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
You might consider reading a basic tutorial to get you started effectively :-)
Upvotes: 7
Reputation: 2613
Isn't JTextArea implementing the Scrollable interface? So why you need JScrollPane?
Edit to your comment, this one works for me:
JScrollPane sP= new JScrollPane(txtarea);
sP.setBounds(10,60,780,500);
sP.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
Upvotes: -1