• 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

JScrollBar/JScrollPane

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a program that displays a JTextArea within a JScrollPane. Basically, when anything is added to the text area, the vertical scroll bar scrolls down to the bottom. I want it to add a vertical scroll bar as soon as the text area becomes bigger than the scroll pane but I dont want the scroll bar to move down to the bottom of the pane. I want the user to be able to see the start of the data in the text area with the choice to scroll down when they wish.

I have the following code:

errorHold = new JScrollPane(errorArea);
errorHold.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);

I dont know if you change a setting on either the:
(a) Scroll pane
(b) scroll bar
(c) text area
(d) viewport

Any help would be greatly appreciated.
 
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To change the scroll position, first retrieve the scrollbar and then use the setValue method, like so:

This should scroll to the top of the view.

However, I am not sure when you need to call this code in order to do what you want. Perhaps you could add an AdjustmentListener to the JScrollBar, but I'm sure there must be a better way...
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//text added to textArea
textArea.setCaretPosition(0);
 
Jo Duffy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent! Thanks that done the trick.
 
This tiny ad is suggesting that maybe she should go play in traffic.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic