By adding
change listener to ScrollPane, you can handle the events on scroll pane.
Ex
scrollPane1.vvalueProperty().addListener((ObservableValue<?
extends Number> ov, Number old_val, Number new_val) -> {
label.setText("Scroll pane 1
changed from " + old_val + " to " + new_val);
});
Find the
below working example.
package com.sample.demos; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.net.MalformedURLException; import javafx.application.Application; import javafx.beans.value.ObservableValue; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.ScrollPane; import javafx.scene.control.ScrollPane.ScrollBarPolicy; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.HBox; import javafx.scene.paint.Color; import javafx.stage.Stage; public class ScrollPaneApp extends Application { private static final String imageFilePath = "C:\\Users\\krishna\\Documents\\Study\\javaFX\\flowers.jpg"; @Override public void start(Stage primaryStage) throws FileNotFoundException, MalformedURLException { ScrollPane scrollPane1 = new ScrollPane(); scrollPane1.setHbarPolicy(ScrollBarPolicy.ALWAYS); scrollPane1.setVbarPolicy(ScrollBarPolicy.ALWAYS); ScrollPane scrollPane2 = new ScrollPane(); scrollPane2.setHbarPolicy(ScrollBarPolicy.ALWAYS); scrollPane2.setVbarPolicy(ScrollBarPolicy.ALWAYS); InputStream is = new FileInputStream(imageFilePath); Image image = new Image(is); scrollPane1.setContent(new ImageView(image)); scrollPane2.setContent(new ImageView(image)); Label label = new Label(); label.setTextFill(Color.RED); label.setWrapText(true); label.maxWidth(100); HBox hBox = new HBox(30, scrollPane1, scrollPane2, label); Scene scene = new Scene(hBox, 800, 200); scrollPane1.vvalueProperty().addListener((ObservableValue<? extends Number> ov, Number old_val, Number new_val) -> { label.setText("Scroll pane 1 changed from " + old_val + " to " + new_val); }); scrollPane2.vvalueProperty().addListener((ObservableValue<? extends Number> ov, Number old_val, Number new_val) -> { label.setText("Scroll pane 2 changed from " + old_val + " to " + new_val); }); /* Set the scene to primaryStage, and call the show method */ primaryStage.setTitle("JavaFX scroll pane app Example"); primaryStage.setScene(scene); primaryStage.show(); } }
TestFX.java
package com.sample.demos; import javafx.application.Application; public class TestFX { public static void main(String args[]) { Application.launch(ScrollPaneApp.class, args); } }
No comments:
Post a Comment