05 January 2011

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

วิธี scroll horizontal scrollbar ด้วยการ กด shift + mouse wheel

// create mouse wheel listener and implement follow below...
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if ((e.getModifiers() & java.awt.event.InputEvent.SHIFT_MASK)
== java.awt.event.InputEvent.SHIFT_MASK) {
if (scrollPane.getHorizontalScrollBar().isShowing()) {
JViewport viewport = scrollPane.getViewport();
Point viewPosition = viewport.getViewPosition();
if (e.getWheelRotation() < 0) { // wheel up -
viewPosition.x -= scrollPane.getHorizontalScrollBar()
.getBlockIncrement() * 15; // adjust up to you!
} else { // wheel down +
viewPosition.x += scrollPane.getHorizontalScrollBar()
.getBlockIncrement() * 15; // adjust up to you!
}
// X axis only
if (viewPosition.x > viewport.getView().getWidth() - viewport.getWidth()) {
viewPosition.x = viewport.getView().getWidth() - viewport.getWidth();
}
if (viewPosition.x < 0) {
viewPosition.x = 0;
}
viewport.setViewPosition(viewPosition);
}
} else { // dispatchEvent of scrollpane
scrollPane.dispatchEvent(e); // scroll vertical normaly...
}
}