• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java stop JScrollPane auto scrolling back to the top when JTextArea is updated

 
Matt Hughes
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a swing application that gets football scores from a website using Jsoup.

When the "Get Scores" button is pressed, the scores are printed to a JTextArea with a JScrollPane.

I have also used a SwingWorker to update the scores every couple of seconds.

My problem is that every time the JTextArea updates, the JScrollPane scrolls back to the top of the text area. I wan the scroll bar to stay where the user left it.

Here is my code (The update is currently set to update every 1 second so you can see what the scroll bar is like).



and here is the main:


Can anyone help?

Thanks.
 
m Korbel
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
search for DefaultCaret.ALWAYS_UPDATE
 
Matt Hughes
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
m Korbel wrote:search for DefaultCaret.ALWAYS_UPDATE


Hi,

I already tried that but it does not work.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a link to another Ranch thread with a similar (but opposite) problem. Perhaps the method mentioned there would fix your problem...

http://www.coderanch.com/t/562291/GUI/java/set-caret-position-beginning-JtextArea
 
Matt Hughes
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Here's a link to another Ranch thread with a similar (but opposite) problem. Perhaps the method mentioned there would fix your problem...

http://www.coderanch.com/t/562291/GUI/java/set-caret-position-beginning-JtextArea


Hi,

That doesn't work either.

I tried this:



and setting the caret position, but it still does not do what i want it to.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. So let's see your SSCCE (<-- follow that link) where you use that method unsuccessfully, and we can go from there. "Doesn't work" isn't really a fruitful basis for discussion in this kind of forum.
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wan the scroll bar to stay where the user left it.


Check out Smart Scrolling. If the scrollbar is at the bottom, then it will automatically scroll as new data is added. If the user has moved the scrollbar somewhere else then it will stay there as new data is added.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!