I can see right now the scrolling priority is given to vertical scrolling. I want to change that. How do I do that?
解决方案
Thanks to the first answer- very helpful. However I found that iNewValue in the response above needs to be multiplied by the evt.getWheelRotation() value which is the number of distinct wheel mouse segments actually rotated by the wheel mouse.
Also the condition of when to scroll needs to take account of this as well - the condition has to be evt.getWheelRotation() <= -1 or evt.getWheelRotation() >= 1
Here is an updated example that worked for me.
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseWheelEvent;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
class MyJScrollPane extends JScrollPane
{
public MyJScrollPane(Component component)
{
super(component);
final JScrollBar horizontalScrollBar = getHorizontalScrollBar();
final JScrollBar verticalScrollBar = getVerticalScrollBar();
setWheelScrollingEnabled(false);
addMouseWheelListener(new MouseAdapter()
{
public void mouseWheelMoved(MouseWheelEvent evt)
{
if (evt.getWheelRotation() >= 1)//mouse wheel was rotated down/ towards the user
{
int iScrollAmount = evt.getScrollAmount();
int iNewValue = horizontalScrollBar.getValue() + horizontalScrollBar.getBlockIncrement() * iScrollAmount * Math.abs(evt.getWheelRotation());
if (iNewValue <= horizontalScrollBar.getMaximum())
{
horizontalScrollBar.setValue(iNewValue);
}
}
else if (evt.getWheelRotation() <= -1)//mouse wheel was rotated up/away from the user
{
int iScrollAmount = evt.getScrollAmount();
int iNewValue = horizontalScrollBar.getValue() - horizontalScrollBar.getBlockIncrement() * iScrollAmount * Math.abs(evt.getWheelRotation());
if (iNewValue >= 0)
{
horizontalScrollBar.setValue(iNewValue);
}
}
}
});
}
}