How do I speed up the scroll speed in a JScrollPane when using the mouse wheel?
Asked Answered
Z

9

115

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?

Zenda answered 7/4, 2011 at 15:21 Comment(1)
Note: For anyone looking for the JavaScript equivalent of this, see Speed up mouse wheel in jScrollPane (jQuery).Hervey
I
231

You can try this :

myJScrollPane.getVerticalScrollBar().setUnitIncrement(16);
Ireland answered 7/4, 2011 at 15:26 Comment(4)
Beautiful. I also noticed this affects the up and down arrows in the scroll bar, which is also very desired.Zenda
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?Oloroso
@Oloroso - seems that scrollbar is set to the mouse position, ignoring unit increment value.Ireland
@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.Applied
S
15

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

scrollPane.getVerticalScrollBar().setUnitIncrement(20);
Shirl answered 7/4, 2011 at 15:26 Comment(0)
M
12

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

yourScrollPane.getVerticalScrollBar().setUnitIncrement(16);
Marti answered 7/4, 2011 at 15:26 Comment(0)
M
11

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

Motion answered 7/4, 2011 at 17:18 Comment(3)
What advantage does this offer that I should include an extra class in my project instead of writing one line of code?Zenda
@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.Motion
The value '3' (units to scroll) comes from the system control panel, which Java honours, enabling you to modify it system-wide.Tiana
T
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.

Twitty answered 7/4, 2011 at 15:25 Comment(0)
I
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

Inapplicable answered 26/2, 2020 at 22:37 Comment(0)
C
-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);

Crespo answered 3/11, 2015 at 20:7 Comment(0)
D
-3

You can also use this.

SwingUtil.setScrollUnitIncrement(yourScrollPane);

Drench answered 24/7, 2013 at 14:30 Comment(1)
hmm ... what is SwingUtil? Assuming the method sets the increment of the given scrollPane - to which value and for which scrollBar?Acerb
H
-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.

Hastate answered 19/6, 2015 at 20:52 Comment(1)
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.Hastate

© 2022 - 2024 — McMap. All rights reserved.