问题
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.
回答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).
回答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!
回答3:
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.
来源:https://stackoverflow.com/questions/1197509/how-do-make-an-infinite-jscrollpane