1

Hi I have made a JTextArea but it is not scrolling, can someone please tell me why?

JTextArea textArea = new JTextArea(2, 0);
textArea.setText("sdsdsd \n dfdfdf \n dsdsdsdsd \n dsdsdsd \n sdsdsdsd");
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
panel.add(textArea);

Also, I would like it to auto scroll down, when new content gets added to, show it only shows the last 2 lines automatically, if possible.

Thanks.

mKorbel
  • 109,107
  • 18
  • 130
  • 305
user3449793
  • 21
  • 1
  • 3

2 Answers2

3

Use,

panel.add(scrollPane);

not

panel.add(textArea);
RKC
  • 1,814
  • 12
  • 13
2

Add the JScrollPane to the JPanel, not the JTextArea.

To Scroll to the bottom, see this answer.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 280,125
  • 25
  • 247
  • 360