Created
January 26, 2012 19:39
-
-
Save jewelsea/1684622 to your computer and use it in GitHub Desktop.
How to listen to ListView scrollevents in JavaFX.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); | |
} | |
} | |
} | |
} |
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
This seems to be broken in JavaFX 8