Posted by Rob Camick on March 3, 2013
There may be times when you have a component in a JScrollPane and data is dynamically added to this component. Normally, you would like the viewport to scroll to the bottom automatically as new data is added so you can see the most recent data. However, there may also be times when you are viewing data somewhere else in the viewport and you don’t want the scrolling to happen automatically. Supporting both of these of these requirement would require a scrollpane to support smart scrolling.
A simple definition for smart scrolling in this situation would be:
- when the viewport is at the bottom and new data is added, then automatically scroll the viewport to the bottom
- when the viewport is not at the bottom and new data is added, then do nothing with the viewport
Maybe not as common, but there may be times when the data is dynamically added at the start of the component. In this case the definition for smart scrolling would be:
- when the viewport is at the top and new data is added, then do nothing with the viewport
- when the viewport is not at the top and new data is added, then adjust the viewport to the relative position it was at before the data was added
The SmartScroller class was created to support both types of smart scrolling. In addition, the SmartScroller class supports vertical or horizontal scrolling. The SmartScroller class only requires a couple of properties to be specified when it is created:
- scroll pane – the scroll pane to monitor
- scroll direction – monitor vertical or horizontal scrolling. Valid values are:
- SmartScroller.VERTICAL (default) – monitor scrolling in the vertical direction
- SmartScroller.HORIZONTAL – monitor scrolling in the horizontal direction
- viewport position – indicates where the viewport should be positioned to see the dynamically added data. Valid values are:
- SmartScroller.END (default) – viewport is kept at the end of the scrollpane (ie. the bottom for vertical scrolling and the right for horizontal scrolling)
- SmartScroller.START – viewport is kept at the start of the scrollpane (ie. the top for vertical scrolling and the left for horizontal scrolling)
For example, in the situation when a JTextArea is used as a simple display console, you can create the SmartScroller by using code like:
JTextArea textArea = new JTextArea(...);
JScrollPane scrollPane = new JScrollPane( textArea );
new SmartScroller( scrollPane );
This convenience constructor will default to vertical scrolling and the viewport will be positioned at the bottom. When you want to use horizontal scrolling you need to use the full constructor. For example:
JTable table = new JTable(...);
JScrollPane scrollPane = new JScrollPane( table );
new SmartScroller(scrollPane, SmartScroller.HORIZONTAL, SmartScroller.END);
Thats all there is to using the SmartScroller class. There are no properties to change at runtime. You must create a new SmartScroller for every scrollpane that requires this functionality.
Try the WebStart demo below. The demo allows you to reset the two properties and observe the behaviour of the SmartScroller.
Although the demo show the usage of a JTextArea and JList you should be able to use the SmartScroller on any scrollpane no matter what component it displays.