title: Auto scroll in javaFX
body: <p>I'm trying to build a Chat Box using javaFX. I'm trying to auto scroll down my page when the current page is filled up. But it doesn't work properly.</p>
  
  <pre><code>import javafx.application.Application;
  import javafx.geometry.Insets;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.scene.control.ScrollPane;
  import javafx.scene.control.TextField;
  import javafx.scene.input.KeyCode;
  import javafx.scene.layout.HBox;
  import javafx.scene.layout.Priority;
  import javafx.scene.layout.VBox;
  import javafx.scene.text.Text;
  import javafx.scene.text.TextFlow;
  import javafx.stage.Stage;
  
  public class ChatBox extends Application{
  final ScrollPane sp = new ScrollPane();
  public static void main(String[] args){
  launch(args);
  }
  public void start(Stage stage){
  TextFlow textFlow = new TextFlow();
  textFlow.setPadding(new Insets(10));
  textFlow.setLineSpacing(10);
  textFlow.setPrefSize(300,300);
  TextField textField = new TextField();
  textField.setPrefSize(50,30);
  Button button = new Button("Send");
  button.setPrefSize(80,30);
  VBox container = new VBox();
  VBox box = new VBox();
  box.getChildren().addAll(sp,textFlow);
  container.setPadding(new Insets(10));
  container.getChildren().addAll(box, new HBox(textField, button));
  VBox.setVgrow(sp, Priority.ALWAYS);
  VBox.setVgrow(textFlow, Priority.ALWAYS);
  textField.prefWidthProperty().bind(container.widthProperty().subtract(button.prefWidthProperty()));
  
  // On Enter press
  textField.setOnKeyPressed(e -&gt; {
      if(e.getCode() == KeyCode.ENTER) {
          button.fire();
      }
  });
  
  button.setOnAction(e -&gt; {
      Text text;
      if(textFlow.getChildren().size()==0){
          text = new Text(textField.getText());
      } else {
          // Add new line if not the first child
          text = new Text("\n" + textField.getText());
      }
      textFlow.getChildren().add(text);
      textField.clear();
      textField.requestFocus();
  });
  VBox vb = new VBox();
  vb.getChildren().addAll(textFlow);
  sp.setVmax(440);
  sp.setPrefSize(400, 300);
  sp.setContent(vb);
  sp.vvalueProperty().bind((ObservableValue&lt;? extends Number&gt;) vb.heightProperty());
  //sp.setPannable(true);
  Scene scene = new Scene(container, 400, 300);
  stage.setScene(scene);
  stage.setTitle("ChatBox");
  stage.show();
  }
  }
  </code></pre>
  
  <p>When i run it , the page goes down</p>
  
  <p><a href="https://i.stack.imgur.com/59Rf7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/59Rf7.png" alt="enter image description here"></a></p>
  
  <p>here hi goes up though it was the first message and the scroll bar is pointing below though its position should be at the top. What should i do?</p>