How do I speed up the scroll speed in a JScrollPan

2019-01-21 09:59发布

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?

8条回答
Animai°情兽
2楼-- · 2019-01-21 10:26

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

scrollPane.getVerticalScrollBar().setUnitIncrement(20);
查看更多
孤傲高冷的网名
3楼-- · 2019-01-21 10:26

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

yourScrollPane.getVerticalScrollBar().setUnitIncrement(16);
查看更多
一夜七次
4楼-- · 2019-01-21 10:32

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.

查看更多
老娘就宠你
5楼-- · 2019-01-21 10:41

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);

查看更多
何必那么认真
6楼-- · 2019-01-21 10:44

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.

查看更多
贪生不怕死
7楼-- · 2019-01-21 10:45

You can try this :

myJScrollPane.getVerticalScrollBar().setUnitIncrement(16);
查看更多
登录 后发表回答