Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Does JavaFX support swipe scrolling?

I’m in the early planning stages of an app in Java that will run on platforms with touch screens, and I want it to offer the kind of touch screen support you see on phones — in particular, the ability to give a quick flick of a swipe on a control such as a list, tree or table view and have it quickly scroll and keep scrolling for an interval even after the swipe has ended. I’ve studied a bunch of JavaFX documentation, and I’m still not sure if that kind of gesture support is offered. Can anyone confirm that it is? Can you point to example code? If not, can anyone suggest an alternate method of getting this kind of behaviour in Java?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You don’t need any special code for this if you are just using standard controls like ListView or TableView, they will support inertial scrolling behavior out of the box.

You can try it on any laptop, by:

  • Putting a lot of things in a ListView.
  • Clicking an item in the ListView to focus it.
  • Scrolling up or down with a gesture on the touchpad.
    • or you can use a touchscreen for the same thing if you have one.

Example: inertia scrolling for ListView

No special code here, it "just works" when given the correct input.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.stage.Stage;

public class SwipeableListApp extends Application {
    private static final int INIT_LINES = 10_000;

    @Override
    public void start(Stage stage) {
        ListView<String> listView = new ListView<>();
        for (int i = 0; i < INIT_LINES; i++) {
            listView.getItems().add(
                    "Item " + (i+1)
            );
        }

        stage.setScene(new Scene(listView));
        stage.show();
    }

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

Example: inertia scrolling for a Pane

If you want to custom handle the swipe gestures, e.g. you are creating a fruit ninja (highly recommended waste of time), then you can use the standard Java event processing options (filters and handlers) to write custom code to react to the gesture events which has inertial support in the GestureEvent and its subclasses (as noted by user EQvan in comments).

When testing this on a Mac and using the touchpad gestures:

  • It won’t detect the swipe events (I think they may be used on actual touch screen devices like tablets or phones).
  • But it will detect the scroll events generated from a touchpad (or mouse wheel, though I didn’t have a wheel to test it), which are also a kind of gesture event.

What you will notice, by looking at the output is that scroll events continue to be generated for a while (with inertia in the event set to true), after you receive the scroll finished event.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class SwipeablePaneApp extends Application {
    @Override
    public void start(Stage stage) {
        Pane pane = new Pane();

        pane.setOnSwipeUp(System.out::println);
        pane.setOnSwipeDown(System.out::println);
        pane.setOnSwipeLeft(System.out::println);
        pane.setOnSwipeRight(System.out::println);

        pane.setOnScrollStarted(System.out::println);
        pane.setOnScroll(System.out::println);
        pane.setOnScrollFinished(System.out::println);

        pane.setPrefSize(200, 200);

        stage.setScene(new Scene(pane));
        stage.show();
    }

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading