Skip to content

Instantly share code, notes, and snippets.

@jewelsea
Created January 26, 2012 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jewelsea/1684622 to your computer and use it in GitHub Desktop.
Save jewelsea/1684622 to your computer and use it in GitHub Desktop.
How to listen to ListView scrollevents in JavaFX.
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class ListViewScrollListener extends Application {
public static void main(String[] args) { launch(args); }
@Override public void start(Stage primaryStage) {
ListView<String> listview = new ListView<>(FXCollections.observableArrayList(
"Orange", "Pear", "Guava", "Pineapple", "Tangerine", "Canaloupe", "Mango", "Banana", "Apple"
));
StackPane layout = new StackPane();
layout.setStyle("-fx-padding: 10; -fx-background-color: cornsilk; -fx-font-size: 25px;");
layout.getChildren().add(listview);
primaryStage.setScene(new Scene(layout, 150, 250));
primaryStage.show();
for (Node node: listview.lookupAll(".scroll-bar")) {
if (node instanceof ScrollBar) {
final ScrollBar bar = (ScrollBar) node;
bar.valueProperty().addListener(new ChangeListener<Number>() {
@Override public void changed(ObservableValue<? extends Number> value, Number oldValue, Number newValue) {
System.out.println(bar.getOrientation() + " " + newValue);
}
});
}
}
}
}
@millmanorama
Copy link

This seems to be broken in JavaFX 8

@InternetPseudonym
Copy link

InternetPseudonym commented May 12, 2017

its also completely useless because of the classes ScrollEvent And ScrollToEvent - just use addEventHandler with ScrollToEvent.scrollToTopIndex() and you're good to go, no need to manipulate internal API

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment