public void mouseDragged(final MouseEvent e){
  int thumbMiddle=0;
  if (!slider.isEnabled())   return;
  currentMouseX=e.getX();
  currentMouseY=e.getY();
  if (!fIsDragging)   return;
  slider.setValueIsAdjusting(true);
switch (slider.getOrientation()) {
case SwingConstants.VERTICAL:
    final int halfThumbHeight=thumbRect.height / 2;
  int thumbTop=e.getY() - offset;
int trackTop=trackRect.y;
int trackBottom=trackRect.y + (trackRect.height - 1);
final int vMax=yPositionForValue(slider.getMaximum() - slider.getExtent());
if (drawInverted()) {
trackBottom=vMax;
}
 else {
trackTop=vMax;
}
thumbTop=Math.max(thumbTop,trackTop - halfThumbHeight);
thumbTop=Math.min(thumbTop,trackBottom - halfThumbHeight);
setThumbLocation(thumbRect.x,thumbTop);
thumbMiddle=thumbTop + halfThumbHeight;
slider.setValue(valueForYPosition(thumbMiddle));
break;
case SwingConstants.HORIZONTAL:
final int halfThumbWidth=thumbRect.width / 2;
int thumbLeft=e.getX() - offset;
int trackLeft=trackRect.x;
int trackRight=trackRect.x + (trackRect.width - 1);
final int hMax=xPositionForValue(slider.getMaximum() - slider.getExtent());
if (drawInverted()) {
trackLeft=hMax;
}
 else {
trackRight=hMax;
}
thumbLeft=Math.max(thumbLeft,trackLeft - halfThumbWidth);
thumbLeft=Math.min(thumbLeft,trackRight - halfThumbWidth);
setThumbLocation(thumbLeft,thumbRect.y);
thumbMiddle=thumbLeft + halfThumbWidth;
slider.setValue(valueForXPosition(thumbMiddle));
break;
default :
return;
}
if (slider.getSnapToTicks()) {
calculateThumbLocation();
setThumbLocation(thumbRect.x,thumbRect.y);
}
}

public void mouseDragged ( final MouseEvent e ) { int thumbMiddle = 0 ; if ( ! slider . isEnabled ( ) ) return ; currentMouseX = e . getX ( ) ; currentMouseY = e . getY ( ) ; if ( ! fIsDragging ) return ; slider . setValueIsAdjusting ( true ) ; switch ( slider . getOrientation ( ) ) { case SwingConstants . VERTICAL : final int halfThumbHeight = thumbRect . height / 2 ; int thumbTop = e . getY ( ) - offset ; int trackTop = trackRect . y ; int trackBottom = trackRect . y + ( trackRect . height - 1 ) ; final int vMax = yPositionForValue ( slider . getMaximum ( ) - slider . getExtent ( ) ) ; if ( drawInverted ( ) ) { trackBottom = vMax ; } else { trackTop = vMax ; } thumbTop = Math . max ( thumbTop , trackTop - halfThumbHeight ) ; thumbTop = Math . min ( thumbTop , trackBottom - halfThumbHeight ) ; setThumbLocation ( thumbRect . x , thumbTop ) ; thumbMiddle = thumbTop + halfThumbHeight ; slider . setValue ( valueForYPosition ( thumbMiddle ) ) ; break ; case SwingConstants . HORIZONTAL : final int halfThumbWidth = thumbRect . width / 2 ; int thumbLeft = e . getX ( ) - offset ; int trackLeft = trackRect . x ; int trackRight = trackRect . x + ( trackRect . width - 1 ) ; final int hMax = xPositionForValue ( slider . getMaximum ( ) - slider . getExtent ( ) ) ; if ( drawInverted ( ) ) { trackLeft = hMax ; } else { trackRight = hMax ; } thumbLeft = Math . max ( thumbLeft , trackLeft - halfThumbWidth ) ; thumbLeft = Math . min ( thumbLeft , trackRight - halfThumbWidth ) ; setThumbLocation ( thumbLeft , thumbRect . y ) ; thumbMiddle = thumbLeft + halfThumbWidth ; slider . setValue ( valueForXPosition ( thumbMiddle ) ) ; break ; default : return ; } if ( slider . getSnapToTicks ( ) ) { calculateThumbLocation ( ) ; setThumbLocation ( thumbRect . x , thumbRect . y ) ; } }
set the models value to the position of the top / left of the thumb relative to the origin of the track .
