88

I'm using JScrollPane to allow scrolling in a JFrame that has a text component that's serving as a text editor. What I want to do, after setting the text in this editor, is have it scroll back up to the top, so you can see what's at the beginning of the file.

Does anyone know how to do this?

7 Answers7

148

Calling setCaretPosition(0) on your text component will cause it to scroll to the top.

Craig
  • 1,519
  • 1
  • 8
  • 3
  • You are right, the problem was that the caret was at the end of the text component. It also works after calling 'setText(...)' on the text editor. (tested with a JEditorPane instance as text panel inside the JScrollPane). – Guillaume Feb 05 '10 at 15:10
41

Just in case you are not using a text component take a look at the thread posted here.... Setting Scroll Bar on a JScrollPane

Their solution is to spin off a thread via invokeLater

final JScrollPane scroll = new JScrollPane(text);
javax.swing.SwingUtilities.invokeLater(new Runnable() {
   public void run() { 
       scroll.getVerticalScrollBar().setValue(0);
   }
});
Community
  • 1
  • 1
Eric Warriner
  • 1,163
  • 10
  • 15
  • 1
    This is the closest answer to my question.. http://stackoverflow.com/questions/42961012/how-to-set-scroll-to-the-top-jpanel-that-has-multiple-jtextareas-inside-jscrol?noredirect=1#comment73017112_42961012 However, how can use this method if there is panel inside JScrollPane? – In-young Choung Mar 22 '17 at 20:07
  • 5
    `invokeLater` doesn't spin off a thread, it just enqueues an event to be executed after the current event (e.g. button click, or whatever is the context of the code which calls `invokeLater`) and any other already-enqueued events finish. – Radiodef Aug 02 '18 at 16:27
10

This will make the work:

DefaultCaret caret = (DefaultCaret) textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
Pb600
  • 130
  • 1
  • 5
9

You can use the method setCaretPosition(0) just after setText(String t) of your text component.

Esteban Herrera
  • 2,263
  • 2
  • 23
  • 32
aosphyma
  • 177
  • 2
  • 2
7

Use JComponent.scrollRectToVisible()

If you need more info, here's an article

ykaganovich
  • 14,736
  • 8
  • 59
  • 96
7

You can try this:

 scrollPane.getViewport().setViewPosition(new Point(0,0));

According to the JavaDocs setViewPosition() behaves like this:

Sets the view coordinates that appear in the upper left hand corner of the viewport, does nothing if there's no view.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
  • 4
    This didn't work for me - the ScrollPane jumps to the top then immediately drops down to the bottom again. – jwoolard Jan 18 '10 at 17:45
  • It worked for me ¯\\_(ツ)_/¯ I have a JTable inside my JScrollPane. – Michael K Oct 27 '16 at 19:21
  • @jwoolard Yes, the solution presented here doesn't work in a number of cases I've encountered. BUT, take note of the solution I posed this morning. – Richard T Apr 13 '18 at 20:56
3

Here's how:

textArea.setSelectionStart(0);
textArea.setSelectionEnd(0); 
Richard T
  • 4,570
  • 5
  • 37
  • 49