How to change Java Swing JScrollPane mouse wheel priority to horizontal scrolling?

不问归期 提交于 2019-12-11 06:48:17

问题


I can see right now the scrolling priority is given to vertical scrolling. I want to change that. How do I do that?


回答1:


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);
                        }
                    }
                }
            });
        }
    }



回答2:


To change the default scrolling priority of the JScrollPane you need to create your own version of JScrollPane and when mouse Wheel is moved enforce the horizontal ScrollBar to come in action instead of vertical ScrollBar.

The overridden version of JScrollPane can be created like this:

import javax.swing.JScrollPane;
import javax.swing.JScrollBar;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseWheelListener;
import java.awt.event.MouseWheelEvent;

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;
                    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;
                    if (iNewValue >= 0)
                    {
                        horizontalScrollBar.setValue(iNewValue);
                    }
                }
            }
        });
    }
}

I Hope this solves your problem of scrolling priority of JScrollPane.



来源:https://stackoverflow.com/questions/14464228/how-to-change-java-swing-jscrollpane-mouse-wheel-priority-to-horizontal-scrollin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!