2

I have a problem implementing the behavior I want in mentioned components. The behavior I want is:
1) By default, when new content is appended to JTextArea, it's auto scrolling to the last inserted line. This one I managed to do with:
DefaultCaret caret = (DefaultCaret)textArea.getCaret(); caret.setUpdatePolicy(DefaultCaret.NEVER_ALWAYS);
2) When user either with mouse or with keyboard moving scrollbar, auto scrolling should be disabled and the text to which user has scrolled is displayed (here I tried to implement AdjustmentListener which on action was setting DefaultCaret.NEVER_NEVER but after setting it to NEVER, I could actually never more scroll to somewhere)
3) When user moves scrollbar to the last inserted line, default behavior mentioned in line 1) should be retaken

The only was I see to do this is somehow calculate caret position depending on document length and the current position of scroll, but to be honest I don't really like this way. I would like to know if there is some other, more elegant and correct method doing this?

Thanks, Serhiy.

EDIT: I have found very similar question and answer to it with source code example. Answer can be found here

Community
  • 1
  • 1
Serhiy
  • 4,073
  • 3
  • 36
  • 66
  • Have you looked at [How to set AUTO-SCROLLING of JTEXTAREA in Java GUI?](http://stackoverflow.com/questions/1627028/how-to-set-auto-scrolling-of-jtextarea-in-java-gui)? – trashgod Aug 30 '11 at 14:26
  • Yep auto scrolling is working fine... But I can't get it stop auto scrolling when user moves scroll bar(or better I can but after that I can't get it moving again :S) – Serhiy Aug 30 '11 at 14:30

1 Answers1

2

This isn't a complete answer, but the comment area is just too limiting ;-)

My guess is that if you really want to hook into the scrollbar behaviour, then you'd have to play very dirty and intercept the ui-installed MouseListener and wrap the default scrollbar actions (in its ActionMap) with your own and implement the toggle of the caret policy there.

On the other hand: I'm not sure you really want it :-) At least assuming the text is always added at the end, because auto-scrolling only ever happens if the text is inserted before the caret location. All your users have to do

  • for stopping automatic scrolling: move the caret away from the end
  • tor allowing automatic scrolling: move the caret to the end
kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • I will try the approach of moving the caret on certain actions with scrollbar, thanks for advice, when I will have some results I'll consider voting up and/or accepting answer. – Serhiy Aug 31 '11 at 22:15