Java Tips Weblog

  • Blog Stats

    • 1,230,170 hits
  • Categories

  • Archives

Smart Scrolling

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.

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.

Try The Demo

Launch – Using Java™ Web Start (JRE 6+ required)

Get The Code

SmartScroller.java

About these ads

4 Responses to “Smart Scrolling”

  1. willemien said

    Hi
    in the sourcecode with this post you do not use the caret option described in

    http://tips4java.wordpress.com/2008/10/22/text-area-scrolling/

    What is the (probably best) way to keep a slider of a JschrollPane at the end of a logger JTextArea?

    also i was wondering
    I have an Swing logger (JDialog, conatining a JscrollPane, containing a JTextPanel) for an process in the main thread and i am wondering what happens if the process adds quicker runnables to the EDT (EventDispachThread, via SwingUtilities, invokeLater() ) than the EDT can process them.

    • Rob Camick said

      This has different functionality than the “Text Area Scrolling” entry. The text area scrolling will reset the scrollpane to the bottom every time new text is added, so that is what you should use if you always want the text at the bottom. This approach does not automatically scroll to the bottom if the user has manually scrolled the scrollpane to look at some other data in the text area.

      The EDT queues all requests. I don’t know what happens it you somehow manage to flood the queue. I would guess the app freezes until all requests can be handled or you get an out of memory exception of some kind.

  2. Mike said

    Thanks for this! I was having issues because I would query the extent property and it wasn’t updating at the same time as maximum updated, you seem to have figured it out.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 95 other followers

%d bloggers like this: