eatBeens
eatBeens

Reputation: 1

JavaFX: EventFilter for two (different) events at the same time

is there a way to handle a MouseEvent.MOUSE_PRESSED event together with a ScrollEvent.ANY? I want to react while both events are triggered. Currently while the mouse is pressed, the scrolling does not take effect.

public class HorizonScroll extends Application {

    public final int SCENE_WIDTH = 800;
    public final int SCENE_HEIGHT = 600;

    @Override
    public void start(Stage stage) {
        Pane root = new Pane();
        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setPrefSize(100,600);
        scrollPane.setHmax(1000);
        scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);



        scrollPane.addEventFilter(MouseEvent.MOUSE_PRESSED, e -> {
            System.out.println("Pressed");
        });


        scrollPane.addEventFilter(MouseEvent.MOUSE_RELEASED, e -> {
            System.out.println("Released");
        });

        scrollPane.addEventFilter(ScrollEvent.ANY, e -> {
            System.out.println("Scrolling");

        });

        

        Scene scene = new Scene(root, SCENE_WIDTH, SCENE_HEIGHT);
        stage.setTitle("HorizonScroll");
        stage.setScene(scene);
        root.getChildren().addAll(scrollPane);


        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }


This is my current dummy. What I want to achieve is something like:

...

scrollPane.addEventFilter(Inputevent.ANY, e -> {
     if(e.getEventType() == SCROLL && e.getEventType() == MOUSE_PRESSED) {
      //scrollPane.setHvalue( + 10 or whatever)

So I want to implement a horizontal scroll with the mousewheel while pressing the mouse button. Thanks in advance!

Upvotes: 0

Views: 158

Answers (1)

DaveB
DaveB

Reputation: 2143

Event handlers are fired on a single event at a time, and the Event can only be of a single type. In order to do what you want, you'll need to establish some kind of State and then toggle it off and on via the event handlers. Since you already have MOUSE_PRESSED and MOUSE_RELEASED captured, it's easiest to establish "is the mouse button pressed" as a state. Then check the state inside the event handler for scrolling.

Like so:

public class HorizonScroll extends Application {

    public final int SCENE_WIDTH = 800;
    public final int SCENE_HEIGHT = 600;
    private BooleanProperty mousePressed = new SimpleBooleanProperty(false);

    @Override
    public void start(Stage stage) {
        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setPrefSize(100, 600);
        scrollPane.setHmax(1000);
        scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);
        scrollPane.addEventFilter(MouseEvent.MOUSE_PRESSED, e -> {
            mousePressed.set(true);
            System.out.println("Pressed");
        });

        scrollPane.addEventFilter(MouseEvent.MOUSE_RELEASED, e -> {
            mousePressed.set(false);
            System.out.println("Released");
        });

        scrollPane.addEventFilter(ScrollEvent.ANY, e -> {
            if (mousePressed.get()) {
                System.out.println("Scrolling - pressed");
            } else {
                System.out.println("Scrolling - not pressed");
            }
        });

        stage.setTitle("HorizonScroll");
        stage.setScene(new Scene(new Pane(scrollPane), SCENE_WIDTH, SCENE_HEIGHT));
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

I will add that the only way I could get the scrolling event handler to trigger was to use the scroll wheel on my mouse. Clicking on the scroll bar and dragging it did not fire the event.

Upvotes: 1

Related Questions