How to use my trackpad for horizontal mouse scrolling in Java AWT ScrollPane

Like many modern mice and trackpads, my laptop supports vertical and horizontal scrolling. This is an exciting feature when you get used to it. I just want my Java applications to support horizontal scrolling through the trackpad / mouse wheel, but everywhere I look, it seems like this is not possible in Java.

I really want someone to tell me that I'm somehow mistaken, this function has already been requested: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6440198

Failure to do this simple thing is actually a transaction breaker for the application I'm working on. In fact, for any application I can imagine! I spent a little time on the Java backend, so I'd really like to find a solution for this seemingly simple thing.

Question: what can I do to implement this behavior? Are raw OS level events even java prone to me, then I would need to write this from scratch?

import java.awt.*;

public class ScrollExample extends Canvas {

    public void paint(Graphics g) {

        g.setColor(Color.green);
        g.fillOval(0,0,400, 400);

    }

    public static void main(String[] args) {

        ScrollExample b = new ScrollExample();
        Frame f = new Frame ("Scroll Example");

        ScrollPane scroller = new ScrollPane (ScrollPane.SCROLLBARS_ALWAYS);
        scroller.add(b,"Center");
        f.setPreferredSize(new Dimension(500,500));
        f.add ("Center",scroller);
        f.pack();
        f.show();

    }

}

The swing example works with both horizontal and vertical scrolling

import java.awt.*;
import javax.swing.*;

public class ScrollExample extends JPanel { 

    public void paint(Graphics g) {

        super.paint(g);

        g.setColor(Color.green);
        g.fillOval(0,0,400, 400);

    }

    public static void main(String[] args) {

        JFrame f = new JFrame ("Scroll Example");
        ScrollExample p = new ScrollExample();
        p.setPreferredSize(new Dimension(1000, 1000));

        JScrollPane scroller = new JScrollPane(p,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scroller.getHorizontalScrollBar().setUnitIncrement(10);
        scroller.getVerticalScrollBar().setUnitIncrement(10);

        f.setPreferredSize(new Dimension(500,500));
        f.add (scroller,BorderLayout.CENTER);
        f.pack();
        f.show();
    }
}
+5
source share
2 answers

Swing, scolling , .

, Swing not AWT.

Edit:

Mouse Wheel Controller . , , .

+1

MacBookPro . , JScrollPane : setWheelScrollingEnabled

+1

All Articles