Reputation: 6476
I would like to change the scroll speed of a JScrollPane
based on its height.
This is how I am currently creating my JScrollPane
:
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.getVerticalScrollBar().setUnitIncrement(scrollSpeed);
The problem is that setUnitIncrement
needs to change the scroll speed dynamically. I am not aware of how I would go about doing that.
Ideally, I would like to scroll by half the height of the scroll pane whenever the user uses mouse wheel button, or presses one of the scroll up or down buttons on the scroll bar.
Upvotes: 0
Views: 180
Reputation: 740
In my experience, the speed at which you scroll through a scroll pane does not (and should not) change dynamically based on the pane's size (e.g. how many elements are in a view port's list or something.)
That said, if you need to, try this:
int getNewScrollSpeed( int scrollPaneHeight ){
// you'll have to play around with the multiplier value to make it work for you
// here I just chose 0.5 as an example
return (int) ( scrollPaneHeight * 0.5 );
}
Then call this routine like this, whenever the scrollPane
's or window's (etc.) height changes.
/* scrollPane height changes occur here */
scrollPane.getVerticalScrollBar().setUnitIncrement(
getNewScrollSpeed ( scrollPane.getHeight() );
Upvotes: 1