• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JScrollBar/JScrollPane

 
Jo Duffy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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.
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • 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...
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//text added to textArea
textArea.setCaretPosition(0);
 
Jo Duffy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent! Thanks that done the trick.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic