Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextArea scrolls to bottom

 
Steffen Foldager
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have a JTextArea within a JScrollPane
If I fill the textarea with too much text (programatically) I want the vertical scrollbar to be in the top.
I've tried

and even

with no luck.
Anyone have an idea?
Thanks,
Steffen
 
Mark Spritzler
ranger
Sheriff
Posts: 17290
9
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

look at the constructors of the JScrollPane, there are 4.
In my above example I used the
JScrollPane(Component view, int vsbPolicy, int hsbPolicy)
the component is what I want to add to the JScrollPane, followed by constants inherited from the ScrollPaneConstants class, which tell what kind of scrollbars to use and when.
Mark
 
Steffen Foldager
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark, but I'm afraid that won't do it.
I am using an empty constructor, but the JScrollPane has the two AS_NEEDED scrollbar policies as default.
Using the three-argument contructor explicitely didn't help either.
But thanks anyway...
Steffen
 
Greg Brouelette
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had to figure out this very problem about 4 years ago. If I remember correctly I think I used the setCaretPosition method in JTextComponent (which is a super class of JTextArea) to set the location of the curser (caret) to the upper left of the text area. That scrolled the text back to the top.
Give that a try. I'm pretty sure that's how I made it work.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Call scrollRectToVisible( new Rectangle( 0, 0, 0, 0 ) ); on the actual Component you want to scroll, not on the JScrollPane...

-Nate
 
Steffen Foldager
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now it's working!!
Nathan's suggestion:

didn't solve the problem. (Sorry, Nate )
Greg's suggestion:

did solve it!
Thanks for the help all of you..
Steffen
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!