Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

TextArea scrollbar problem  RSS feed

 
satya sahu
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am adding some text to the textarea. The textarea is added to a scrollpane. If the text increases the displayable area then the scroll bar is displayed automatically and the Vertical scrollbar lies on the top.

But in my case after adding the text in the textarea, the scrollbar is scrolled to the bottom. Same is happening for Horizontal scrollbar, it is displayed to the right instead of left.

Why such thing is happening.
And any solution for this.

Thanks in advance.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Testing in j2se 1.5.0 shows that the vertical scrollbar is at the bottom of its track after each text addition for both JTextArea and TextArea (you did not specify Swing or AWT components). The horizontal scrollbar is all the way to the left in JScrollPane and the ScrollPane scrollbar seems to stay wherever it was placed before the text addition.
It isn't clear from your post what behavior you want.
If you want the scrollbars to be positioned at the beginning of the document you can use setCaretPoistion(0) for each component.
Also, Swing has the JComponent method scrollRectToVisible(Rectangle aRect) (in the JTextArea api) that you can call or override.
For example, say if you want to scroll to the position of the text just added you could save the position of the end of the document before adding the new text,

append the text, use the JTextComponent method modelToView(int pos) to get a Rectangle that you can work with to pass to scrollRectToView. You can use getParent to access the JViewport for size information and use this to accurately specify your Rectangle for scrolling.
A more drastic approach is to get a reference to one or both scrollbars and to programatically set them according to your need.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!