How to use my trackpad for horizontal mousewheel scrolling in a Java AWT ScrollPane
Asked Answered
N

2

6

Like many modern mice and trackpads, my laptop supports vertical and horizontal scrolling. It's an addictive feature once you get used to it. I simply want my Java apps to support horizontal scrolling via the trackpad/mousewheel, but everywhere I search it seems that this is not possible in Java.

I really want someone to tell me that I'm somehow doing it wrong, this feature is already requested behaviour: https://bugs.java.com/bugdatabase/view_bug?bug_id=6440198

The inability to do this simple thing is actually a deal breaker for the app I'm working on. In fact, for any app I can envision! I've invested a bit of time in the Java backend, so I'd really like to find a solution for this seemingly simple thing.

Question is what could I do to implement this behaviour? Are raw OS level events even exposed to me by java, would I then 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();

    }

}

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();
    }
}
Nutrilite answered 16/5, 2010 at 10:18 Comment(0)
P
1

This is supported by default in Swing although vertical scolling has priority over horizontal scrolling when both scrollbars are visible.

I would expect new development to be done in Swing not AWT.

Edit:

You should be able to use the Mouse Wheel Controller to customize the scroll speed. I've never tried it on horizontal scrolling, let me know if it works.

Poleaxe answered 16/5, 2010 at 15:51 Comment(3)
Thanks, Swing works, albeit more slowly that AWT, but that might just be the default configuration of the wheel. I was really wanting to use AWT for the OS controls, but I'll now switch to swing instead, most of my UI will be overridden by me anyway.Nutrilite
Regarding the comment about slow scrolling, see my edit above.Poleaxe
Thanks for the update. I tried that Mouse Wheel Controller class, but it made Scrolling sluggish and jerky on my hardware. My final solution was to use the setUnitIncrement() method on the ScrollBars themselves, I've added the details to the code above.Nutrilite
L
1

The navigation events on my MacBookPro's trackpad are handled out of the box. Note there is a method on JScrollPane to enable wheel events: setWheelScrollingEnabled

Leanoraleant answered 16/5, 2010 at 16:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.