Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically scroll to the bottom of a text area

I have a text area with scroll bar. At regular intervals, I am adding new lines of text to it. I would like the text area to automatically scroll to the bottom-most entry (the newest one) whenever a new line is added. How can I accomplish this?

textAreaStatus = new WebTextArea();
scrollPane = new JScrollPane(textAreaStatus);
textAreaStatus.setBackground(Color.black);
textAreaStatus.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
like image 760
itro Avatar asked Jan 25 '12 09:01

itro


People also ask

How do you scroll to the bottom of a page automatically?

To use you just need to press CTRL+ Left click of your mouse and drag the mouse a bit in the direction you want to scroll the page. For example, if you want to scroll up to the page automatically, click CTRL+ left click and slightly move your mouse upwards, the tool will start scrolling up the page.


2 Answers

Have a look at the updatePolicy property of DefaultCaret: it might do what you want

DefaultCaret caret = (DefaultCaret) textArea.getCaret();
caret.setUpdatePolicy(ALWAYS_UPDATE);

A nice summary of options by Rob (@camickr)

like image 61
kleopatra Avatar answered Oct 01 '22 08:10

kleopatra


textArea.setCaretPosition(textArea.getDocument().getLength());
like image 21
StanislavL Avatar answered Oct 01 '22 08:10

StanislavL