1

I have a Scrollable-VBox with a lot of TextFields in it. I can TAB-Browse from one TextField to the next. When I reach the last currently visible and press TAB again, the scrollpane switches to the next "page" and the cursor is in the new uppermost Textfield.

But I need a line-by-line scrolling behaviour while jumping from Text-Field to the next at the bottom of the visible area.

Anybody with an idea.

  package test;

  import javafx.application.Application;
  import javafx.scene.Scene;
  import javafx.scene.control.ScrollPane;
  import javafx.scene.control.TextField;
  import javafx.scene.layout.HBox;
  import javafx.scene.layout.VBox;
  import javafx.stage.Stage;

  public class Scroller extends Application {

  @Override
  public void start(Stage primaryStage) {

    VBox vb = new VBox();
    for (int i = 0; i < 360; i++) {
        TextField x = new TextField(String.valueOf(i));
        vb.getChildren().add(x);
    }
    ScrollPane sp = new ScrollPane(vb);
    sp.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
    sp.setFitToWidth(true);

    TextField tf = new TextField();
    HBox root = new HBox();
    root.getChildren().addAll(sp, tf);

    Scene scene = new Scene(root, 300, 250);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
  }

  public static void main(String[] args) {
    launch(args);
}
}
SedJ601
  • 12,173
  • 3
  • 41
  • 59
JustMe
  • 366
  • 1
  • 4
  • 14

1 Answers1

2

The coordinates of the top in content coordinates can be determined as

topY = (contentHeight - viewportHeight) * vValue

You could use this formula and a listener for the focusedNode property of a scene to scroll the bottom of the focused node to the bottom of the viewport:

public static boolean isChild(Parent parent, Node node) {
    while (node != null && node != parent) {
        node = node.getParent();
    }
    return parent == node;
}

@Override
public void start(Stage primaryStage) {
    VBox vb = new VBox();
    for (int i = 0; i < 360; i++) {
        TextField x = new TextField(String.valueOf(i));
        vb.getChildren().add(x);
    }
    ScrollPane sp = new ScrollPane(vb);
    sp.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
    sp.setFitToWidth(true);

    TextField tf = new TextField();
    HBox root = new HBox();
    root.getChildren().addAll(sp, tf);

    Scene scene = new Scene(root, 300, 250);
    scene.focusOwnerProperty().addListener((o, oldVal, newVal) -> {
        if (isChild(vb, newVal)) {
            // get bounds of focused node in ScrollPane content
            Bounds bounds = newVal.getLayoutBounds();
            while (newVal != vb) {
                bounds = newVal.localToParent(bounds);
                newVal = newVal.getParent();
            }
            double h = vb.getHeight();
            double vH = sp.getViewportBounds().getHeight();

            // scroll node to bottom of the viewport if bottom is not in view
            if (bounds.getMaxY() > sp.getVvalue() * (h - vH) + vH) {
                sp.setVvalue((bounds.getMaxY() - vH) / (h - vH));
            }
        }
    });

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
}
fabian
  • 80,457
  • 12
  • 86
  • 114