• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

TextArea scrollbar problem

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic