05 January 2011

JAVA : How to scroll the horizontal scrollbar by (shift + mouse wheel)

วิธี scroll horizontal scrollbar ด้วยการ กด shift + mouse wheel
  1. // create mouse wheel listener and implement follow below...  
  2. @Override  
  3. public void mouseWheelMoved(MouseWheelEvent e) {  
  4.     if ((e.getModifiers() & java.awt.event.InputEvent.SHIFT_MASK)  
  5.             == java.awt.event.InputEvent.SHIFT_MASK) {  
  6.         if (scrollPane.getHorizontalScrollBar().isShowing()) {  
  7.           JViewport viewport = scrollPane.getViewport();  
  8.           Point viewPosition = viewport.getViewPosition();  
  9.           if (e.getWheelRotation() < 0) { // wheel up -  
  10.              viewPosition.x -= scrollPane.getHorizontalScrollBar()  
  11.                     .getBlockIncrement() * 15// adjust up to you!  
  12.           } else { // wheel down +  
  13.              viewPosition.x += scrollPane.getHorizontalScrollBar()  
  14.                     .getBlockIncrement() * 15// adjust up to you!  
  15.           }  
  16.           // X axis only  
  17.           if (viewPosition.x > viewport.getView().getWidth() - viewport.getWidth()) {  
  18.               viewPosition.x = viewport.getView().getWidth() - viewport.getWidth();  
  19.           }  
  20.           if (viewPosition.x < 0) {  
  21.               viewPosition.x = 0;  
  22.           }  
  23.           viewport.setViewPosition(viewPosition);  
  24.         }  
  25.     } else { // dispatchEvent of scrollpane  
  26.         scrollPane.dispatchEvent(e); // scroll vertical normaly...  
  27.     }  
  28. }