By adding change listner to scroll bar, you can catch the events of scroll bar.
Ex
scrollBar.valueProperty().addListener((ObservableValue extends Number> ov, Number old_val, Number new_val) -> {
label.setText("Scrollbar value changed from " + old_val + " to " + new_val);
});
Find the below working application.
package com.sample.demos;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Orientation;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class ScrollBarApp extends Application {
@Override
public void start(Stage primaryStage) throws FileNotFoundException, MalformedURLException {
ScrollBar scrollBar = new ScrollBar();
scrollBar.setMin(0);
scrollBar.setOrientation(Orientation.VERTICAL);
scrollBar.setPrefHeight(180);
scrollBar.setMax(360);
scrollBar.setUnitIncrement(30);
scrollBar.setBlockIncrement(35);
Label label = new Label();
label.setTextFill(Color.RED);
HBox hBox = new HBox(30, scrollBar, label);
Group root = new Group();
root.getChildren().addAll(hBox);
Scene scene = new Scene(root, 500, 300);
scrollBar.valueProperty().addListener((ObservableValue extends Number> ov, Number old_val, Number new_val) -> {
label.setText("Scrollbar value changed from " + old_val + " to " + new_val);
});
/* Set the scene to primaryStage, and call the show method */
primaryStage.setTitle("JavaFX scroll bar 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(ScrollBarApp.class, args);
}
}
Previous Next Home
This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here