5

I have a scrollPane which has various TitledPane into this, each TitledPane has various textfield's as children. When i navigate throughout textfield's using Tab key, the scrollpane do not autoscroll to ensure visible the control gaining focus. This is a program demonstrating the problem.

import java.util.Random;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ScrollPaneEnsureVisible extends Application {

    private static final Random random = new Random();

    @Override
    public void start(Stage primaryStage) {

        VBox root = new VBox();
        ScrollPane scrollPane = new ScrollPane();
        Pane content = new Pane();
        scrollPane.setContent(content);

        for (int i = 0; i < 3; i++) {
            TitledPane pane = new TitledPane();
            AnchorPane paneContent = new AnchorPane();
            for (int j = 0; j < 2; j++) {
                TextField text = new TextField();
                paneContent.getChildren().add(text);
                text.setLayoutX(8);
                text.setLayoutY(j*30);
            }
            pane.setCollapsible(false);
            pane.setContent(paneContent);
            pane.setLayoutY(i*100);
            content.getChildren().add(pane);
        }

        root.getChildren().add(scrollPane);

        scrollPane = new ScrollPane();
        content = new Pane();
        scrollPane.setContent(content);
        for (int i = 0; i < 3; i++) {
            TitledPane pane = new TitledPane();
            AnchorPane paneContent = new AnchorPane();
            pane.setContent(paneContent);
            pane.setPrefWidth(200);
            pane.setPrefHeight(80);
            pane.setCollapsible(false);
            pane.setLayoutY(i*100);
            content.getChildren().add(pane);
        }

        root.getChildren().add(scrollPane);


        Scene scene = new Scene(root, 300, 250);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) { launch(); }
}

For the second titledpane all is fine, but for first not. i need to sensure visible (autoscroll the scrollpane) that controls when they gain focus.

kato2
  • 535
  • 4
  • 15
  • Does this answer your question? [How to scroll to make a Node within the content of a ScrollPane visible?](https://stackoverflow.com/questions/12837592/how-to-scroll-to-make-a-node-within-the-content-of-a-scrollpane-visible) – Sergey Grinev Nov 23 '19 at 12:27

0 Answers0