I have created a textarea, and i need a scrollbar applied to the textarea when necessary (when the text gets too long down and it cant be read anymore).
this is the code i have written, but for some reason, the scrollbar doesnt really come up?
final JTextArea textArea = new JTextArea();
textArea.setEditable(false);
textArea.setBounds(10, 152, 456, 255);
textArea.setBorder(border);
textArea.setLineWrap(true);
sbrText = new JScrollPane(textArea);
sbrText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
panel_1.add(textArea);
You have to remove the code line that makes the
JTextArea
have absolute size on the screen due to usingsetBounds()
. This makes it non-resizable, andJScrollPane
works only if its content is resizable.Please read JTextArea and JScrollPane tutorial; please run examples from both tutorials.
Make sure that the
preferredSize
and theviewportSize
are the same. The scrollpane will size itself with the preferred size of thetextArea
, and this can cause the scrollbars to disappear if the preferred size of the text area is large enough to display itself.Again, please read the JTextArea and JScrollPane tutorials.
You added the TextArea to a parent twice (scrollPane and panel). Change your last line to
see this
from http://forum.html.it/forum/showthread/t-1035892.html