Java Swing - Using JScrollPane and Having it scroll back to top
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?
Calling setCaretPosition(0) on your text component will cause it to scroll to the top.
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);
}
});