JavaFX - Pannable ListView

Viewed 127

I have noticed that ListView does not behave the same as ScrollPane in terms of scrolling on Embedded Devices with touch. I cannot find a way to make ListView pannable, so as to make it scroll when dragging, and bounce back when off bounds (see example).

enter image description here

You can simulate touching by setting the JVM arguments below.

-Dcom.sun.javafx.isEmbedded=true
-Dcom.sun.javafx.touch=true
public class ScrollTouch extends Application  {

  @Override
  public void start(Stage primaryStage) throws Exception { 
    primaryStage.setTitle("ScrollBar VS ListView");

    // ScrollPane
    ScrollPane scrollPane = new ScrollPane();
    VBox spContent = new VBox();
    for(int i=0; i<20; i++)
     spContent.getChildren().add(getLabel("Label No." + i));
    
    scrollPane.setPrefWidth(150);
    scrollPane.setContent(spContent);
    scrollPane.setPannable(true);

    // ListView
    ListView<Label> listView = new ListView<Label>();
    for(int i=0; i<20; i++)
     listView.getItems().add(getLabel("Label No." + i));
    
    listView.setFocusTraversable(false);
    
    HBox rootPane = new HBox();
    rootPane.getChildren().addAll(scrollPane,listView);

    Scene scene = new Scene(rootPane, 400, 300);
    primaryStage.setScene(scene);
    primaryStage.show();
  }
  
  private Label getLabel(String text) {
      Label label = new Label(text);
      label.setPrefHeight(20);
      return label;
  }

  public static void main(String[] args) {
    Application.launch(args);
  }
}
0 Answers
Related