1

//created a JScrollPane and when i run it the scrollbars will automatically go to the most bottom of my textarea, but i need it to be on top. thank you in advance! =)

JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(22, 86, 262, 57);
content.add(scrollPane);

//the text area used

JTextArea txtrTryingTryingTrying = new JTextArea();
scrollPane.setViewportView(txtrTryingTryingTrying);
txtrTryingTryingTrying.setText("-------");
  • possible duplicate of [Java Swing - Using JScrollPane and Having it scroll back to top](http://stackoverflow.com/questions/291115/java-swing-using-jscrollpane-and-having-it-scroll-back-to-top) – Gilbert Le Blanc Jan 23 '15 at 20:15

1 Answers1

0

I've been trying as well, and found a solution.

There is a useful method called: setCaretPosition(int)

After you use

component.setText("Text here");

You must use

component.setCaretPosition(0); //0 will set it to the top

In your case, you must add setCaretPosition() after setText

JTextArea txtrTryingTryingTrying = new JTextArea();
scrollPane.setViewportView(txtrTryingTryingTrying);
txtrTryingTryingTrying.setText("-------");
txtrTryingTryingTrying.setCaretPosition(0);
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Joshua Lochner
  • 153
  • 1
  • 9