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 using setBounds(). This makes it non-resizable, and JScrollPane works only if its content is resizable.
// wrong
textArea.setBounds(10, 152, 456, 255);
Please read JTextArea and JScrollPane tutorial; please run examples from both tutorials.