2

I have a few text areas that are filled with loops of information. Is it possible to make it 'jump' or scroll back to the top automatically after the loop has completed, so that the user sees it from the start and not the end?

user
  • 6,897
  • 8
  • 43
  • 79
Mark Tickner
  • 1,023
  • 2
  • 15
  • 26

3 Answers3

6

You can use setCaretPosition() to set the cursor at position 0 of the TextArea. That usually works for me.

tckmn
  • 57,719
  • 27
  • 114
  • 156
Magnus
  • 61
  • 1
  • 2
0

Use setUpdatePolicy with NEVER_UPDATE, and make sure the setUpdatePolicy comes before setting any text to the JTextArea. In my example I initialize a JTextArea without any text and call setUpdatePolicy right after.

JTextArea jTextArea = new JTextArea();
((DefaultCaret)jTextArea.getCaret()).setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
-2

I have solved this problem by creating a new TextArea which acts as a sort of buffer (and isn't displayed). The loop writes to this area and when completed gets the text from that and sets it to the required TextArea.

Mark Tickner
  • 1,023
  • 2
  • 15
  • 26