aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JScrollBar/JScrollPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JScrollBar/JScrollPane" Watch "JScrollBar/JScrollPane" New topic
Author

JScrollBar/JScrollPane

Jo Duffy
Greenhorn

Joined: Jul 26, 2005
Posts: 2
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

Joined: Apr 21, 2005
Posts: 410
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

Joined: Jun 09, 2003
Posts: 4632
//text added to textArea
textArea.setCaretPosition(0);
Jo Duffy
Greenhorn

Joined: Jul 26, 2005
Posts: 2
Excellent! Thanks that done the trick.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JScrollBar/JScrollPane