3

I've implemented drag scroll before, but what's the best way to go about creating an infinite scroll pane? Of course there won't be any scrollbars and I will implement drag scroll.

What I am trying to do is implement dynamic loading on an infinite surface.

EDIT

Of course it wouldn't actually be infinite. I am asking how to fake it.

Pyrolistical
  • 27,624
  • 21
  • 81
  • 106
  • How would it behave? Could you drag the thumb to the bottom? Would it sit in the middle of the scrollbar? – akf Jul 29 '09 at 00:40
  • Google reader sort of does this, when you scroll to the bottom sixth or so it loads the next tranche and the scrollbar is adjusted accordingly – Rich Seller Jul 29 '09 at 00:42

3 Answers3

2

You could do the following:

AdjustmentClass adj = new AdjustmentClass();
scrollPane.getHorizontalScrollBar().addAdjustmentListener(adj);
scrollPane.getVerticalScrollBar().addAdjustmentListener(adj);

And in your AdjustmentClass do the following:

public class AdjustmentClass implements AdjustmentListener {
    public void adjustmentValueChanged(AdjustmentEvent adjustmentEvent) {
        // Implement your custom code here
    }
}

To get orientation of the event do:

adjustmentEvent.getAdjustable().getOrientation();

You find the orientation constants in:

AdjustmentEvent.UNIT_INCREMENT
AdjustmentEvent.BLOCK_INCREMENT

etc. etc.

To check if user is dragging the scroll-bar knob do:

adjustmentEvent.getValueIsAdjusting()

There are i few more, check the API for more info if you need.

Happy coding!

Bones
  • 21
  • 2
1

You could use JScrollPane.getViewport() to get the JViewport and add a ChangeListener to it. The JViewport will alert your listener when the size or, more importantly the position has changed. If the position has changed close to the edge of the JScrollPane you can then resize the viewport (you will need to disregard the size-triggered change event).

akf
  • 38,619
  • 8
  • 86
  • 96
0

In a bit of a delay, I'll post the answer here that I gave on a related question:

What you can do, is use the way GIMP has a scroll area that can extend as you move - catch the click of the middle mouse button for marking the initial intention to move the viewport. Then, when the mouse is dragged (while the button is clicked) move the viewport of the jscrollpane by the offset between the initial click and the current position. If we moved outside the bounds of the canvas, then you should simply enlarge the canvas.

Where the canvas is the component displayed inside the scrollpane. Note that when moving the viewport you may want to do the following:

  • If the previous and current viewports are inside the minimal area for display (i.e. the actual area that would have been taken by the widget if it was flat and not in a scroll pane), do nothing
  • If the current area isn't inside the "minimal area", then resize the internal widget to the size of the rectangle containing both the desired viewport and the original widget (treat the original widget as a rectangle with top-left corner at (0,0)). Note that you will need to change the scroll position of the widget.
Community
  • 1
  • 1
Barak Itkin
  • 4,872
  • 1
  • 22
  • 29