112

I see the method JScrollPane.setWheelScrollingEnabled(boolean) to enable or disable the mouse wheel scrolling. Is there any way to adjust the speed of the scrolling, though? It is, in my opinion, ludicrously slow. No matter what size I make the window, the scrolling is about three pixels per click. I'd like it to be much more than that.

Any ideas?

Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
  • 5
    Note: For anyone looking for the Java*Script* equivalent of this, see [Speed up mouse wheel in jScrollPane (jQuery)](http://stackoverflow.com/questions/17154760/speed-up-mouse-wheel-in-jscrollpane-jquery). – Boann Oct 11 '13 at 08:05

9 Answers9

226

You can try this :

myJScrollPane.getVerticalScrollBar().setUnitIncrement(16);
Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
StKiller
  • 7,631
  • 10
  • 43
  • 56
  • 2
    Beautiful. I also noticed this affects the up and down arrows in the scroll bar, which is also very desired. – Erick Robertson Apr 07 '11 at 15:44
  • How would you also make this affect clicking in the area between the scroll box and the up or down arrows in the scroll bar? – Rune Aug 28 '13 at 17:07
  • @Rune - seems that scrollbar is set to the mouse position, ignoring unit increment value. – StKiller Aug 28 '13 at 17:39
  • @AndreiPodoprîgora You should note that changing this value only changes the scroll speed when scrolling with a mouse wheel (or a different device that allows scrolling), the arrow keys, and the scroll bar arrows. This value won't affect the speed the scroll pane is moving with when dragging the scroll bar knob around. It's very hard to change this value. If you want to achieve this, you should add custom adjustment listeners to the scrollbar and change it's values. – Tim Visée Nov 23 '13 at 14:25
15

One way would be to set the unit increment of the scrollbar to a larger number:

scrollPane.getVerticalScrollBar().setUnitIncrement(20);
Jeff
  • 3,669
  • 1
  • 23
  • 33
12

You can do this by setting the unit increment for a ScrollBar. See the example.

yourScrollPane.getVerticalScrollBar().setUnitIncrement(16);
11

If you want to set the mouse wheel scroll amount indepedent of the scrollbar unit amout you can use the Mouse Wheel Controller.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • What advantage does this offer that I should include an extra class in my project instead of writing one line of code? – Erick Robertson Feb 26 '12 at 16:03
  • 3
    @ErickRobertson, it was just intented to give you added flexibility. The default is for the scrolling to be 3 times the unit increment value. For example with a JTable the unit increment defaults to the row height and default scrolling will therefore be 3 rows. If for some reason your requirement is to scroll a page at a time or 5 rows at a time you cant to that by just manipulating the unit value. Also the default row height may be different for each LAF. By manipulating the scroll amount on a relative basis you have better cross platform support. – camickr Apr 15 '12 at 19:40
  • 1
    The value '3' (units to scroll) comes from the system control panel, which Java honours, enabling you to modify it system-wide. – Luke Usherwood Aug 05 '16 at 09:59
2

A quick search brought up this page: How to increase the JScrollPane scrolling speed for mousewheel users. It turns out that the scrolling increment is a property of the scroll bar itself (JScrollBar.setUnitIncrement) and not the scroll pane.

casablanca
  • 69,683
  • 7
  • 133
  • 150
0

For more precise control, the component being scrolled can implement the Scrollable interface. This allows you to dynamically calculate the scrolling unit size (arrow buttons and arrow keys) and the scrolling block size (mouse wheel).

How to use Scroll Panes

Brad Mace
  • 27,194
  • 17
  • 102
  • 148
-2

I was trying to find a better method to read through 32000 lines in my ScrollPane

try this

scrollPane.getVerticalScrollBar().setUnitIncrement(100); scrollPane.getViewport().putClientProperty("EnableWindowBlit", Boolean.TRUE); scrollPane.getViewport().setScrollMode(JViewport.BACKINGSTORE_SCROLL_MODE);

-3

You can also use this.

SwingUtil.setScrollUnitIncrement(yourScrollPane);

user2287966
  • 257
  • 1
  • 4
  • 10
  • 1
    hmm ... what is SwingUtil? Assuming the method sets the increment of the given scrollPane - to which value and for which scrollBar? – kleopatra Jul 24 '13 at 14:41
-3

My solution to speeding up the scroll:

  1. Add scrollbar's parameter:

    scrollPane.getVerticalScrollBar().putClientProperty("JScrollBar.fastWheelScrolling", true);

  2. Implement a wheel listener (on the component inside jViewport):

    public void mouseWheelMoved(MouseWheelEvent e) {
        boolean isCtrl = (e.getModifiersEx() & MouseWheelEvent.CTRL_DOWN_MASK) != 0;
        boolean isShift = (e.getModifiersEx() & MouseWheelEvent.SHIFT_DOWN_MASK) != 0;
    
        MouseWheelEvent eventToDispatch = e;
        if (isCtrl || isShift) {
            int amountMulti = 1;
            int rotMulti = 1;
            if (isCtrl) {
                amountMulti *= 10;
                if (isShift) {
                    amountMulti *= 5;
                    rotMulti *= 2;
                }
            }
            int mod = e.getModifiers() & ~InputEvent.CTRL_MASK & ~InputEvent.SHIFT_MASK;
            int modEx = e.getModifiersEx() & ~MouseWheelEvent.CTRL_DOWN_MASK & ~MouseWheelEvent.SHIFT_DOWN_MASK;
            eventToDispatch = new MouseWheelEvent(this, e.getID(), e.getWhen()
             , mod | modEx, e.getX(), e.getY()
             , e.getXOnScreen(), e.getYOnScreen(), e.getClickCount(), e.isPopupTrigger()
             , e.getScrollType(), e.getScrollAmount()*amountMulti, e.getWheelRotation()*rotMulti
             , e.getPreciseWheelRotation()*amountMulti*rotMulti);
        }
    
        getParent().dispatchEvent(eventToDispatch);
    }
    

    The increase of wheelRotation is necessary: otherwise the number of scrolled lines will be limited to the size of the screen.

rychu
  • 896
  • 1
  • 7
  • 16
  • I've fixed the code so the SHIFT and CTRL flags are removed to prevent horizontal scrolling. I'm sure the jdk guys have added horizontal scrolling with shift key to the jdk just after my original post. Downvoters: please remove your downvotes if the solution is working for you now or post a comment and explain what is still missing. – rychu Jun 27 '17 at 05:04