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

前端 未结 9 753
庸人自扰
庸人自扰 2020-12-12 23:03

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 scro

9条回答
  •  执念已碎
    2020-12-12 23:48

    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.

提交回复
热议问题