Search code examples
javaswingmouseeventnetbeans-platformjscrollbar

mouse wheel listener not working in jscrollbar


I am developing a desktop application in netbeans platform (netbeans module) in which i have a desktoppane and a jscrollbar. i have implemented MouseWheelListener and added

scrollBar.addMouseWheelListener(this);

in the constructor of the class. now when i am scrolling the wheel of mouse it do not scroll the scroll bar though i am getting values in the

 private void scrollBarMouseWheelMoved(java.awt.event.MouseWheelEvent evt) { 


 System.out.println("mouse value is------------ " + evt.paramString());
}

ouput of above sout is

mouse value is------------ MOUSE_WHEEL,(8,49),absolute(0,0),button=0,clickCount=0,scrollType=WHEEL_UNIT_SCROLL,scrollAmount=3,wheelRotation=1

what should i do now to enable mosue wheel event on jscrollbar?

I have searched but i found events for scrollpane but i am looking for scrollbar explicitly..

i have removed extra code and shown what i am looking for in the following sample code

    public final class ScrollableWindow1TopComponent extends TopComponent implements ComponentListener, MouseWheelListener {

    private javax.swing.JScrollBar scrollBar;
    private javax.swing.JDesktopPane scrollableGraphnewContainer;

    public ScrollableWindow1TopComponent() {
      this.addComponentListener(this);
      scrollBar.addMouseWheelListener(this);

    }
     private void scrollBarMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {                                          
       System.out.println("mouse value is------------ " + evt.paramString());
    }
     private void scrollBarAdjustmentValueChanged(java.awt.event.AdjustmentEvent evt) { 
      //code that works fine 
    }
  }

Solution

  • my actual problem was that i was not getting the scroll value changed on mouse wheel change but i was getting the mouse wheel event so i have solved as follow:

     private void scrollBarMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {                                          
            if (evt.getUnitsToScroll() > 0) {
                scrollBar.setValue(scrollBar.getValue() + 1);
            } else {
                scrollBar.setValue(scrollBar.getValue() - 1);
            }
       }      
    

    everytime i scroll wheel up side evt.getUnitsToScroll() gives positive value and for down it gives negative value so i incremented the value of scrollbar and rest of thing are automatically handled by

      private void scrollBarAdjustmentValueChanged(java.awt.event.AdjustmentEvent evt) { 
         //my code...
    }