title: Autoscroll JavaFX TextFlow
body: <p>I have a JavaFX <code>TextFlow</code> wrapped in a <code>ScrollPane</code>, and I am trying to get it to automatically scroll to the bottom whenever a new <code>Text</code> is added to the <code>TextFlow</code>.</p>
  
  <p>I have tried attaching listeners maxing the <code>ScrollPane</code>'s <code>vvalue</code> to:</p>
  
  <ul>
  <li>The <code>ScrollPane</code>'s <code>vvalue</code> property itself.
  <ul>
  <li>This locks the <code>ScrollPane</code> to the bottom, which is not desired. </li>
  </ul></li>
  <li>The <code>ScrollPane</code>'s <code>viewportBounds</code> property.
  <ul>
  <li>This just doesn't work.</li>
  </ul></li>
  <li>The <code>TextFlow</code>'s children list.
  <ul>
  <li>This scrolls to the location that <em>was</em> the bottom <em>before</em> the latest <code>Text</code> was added, strangely. I have tried explicitly requesting layout before scrolling, but this had no effect</li>
  </ul></li>
  </ul>
  
  <p>For an extra big thank you, I'd like to scroll such that if the new addition is too large to display at once, the <code>ScrollPane</code> scrolls such that the new addition is at the top, and the user should scroll manually to see the 'overflow'. As I said, this would be an added bonus, just scrolling to the bottom would be just fine as I don't expect such large additions (regularly).</p>
  
  <p>And no, I'm not switching to a <code>TextArea</code>, in which this would be relatively simple. I want to be able to easily add regular, bold and italic text to the <code>TextFlow</code>, and <code>TextArea</code> doesn't support this. I also tried Thomas Mikula's <a href="https://github.com/TomasMikula/RichTextFX" rel="nofollow">RichTextFX</a>, but</p>
  
  <ol>
  <li>It kept throwing <code>StackOverflowError</code>s on internal code without explanation.</li>
  <li>I don't really want to use third-party libraries for this project.</li>
  </ol>
  
  <p>So any solution that will work with <code>TextFlow</code> would be greatly appreciated.</p>
  
  <p>EDIT: Solution, as requested:</p>
  
  <pre><code>private ScrollPane textContainer;
  private TextFlow text;
  
  public BaseGui() {
      //....
      text.getChildren().addListener(
                  (ListChangeListener&lt;Node&gt;) ((change) -&gt; {
                      text.layout();
                      textContainer.layout();
                      textContainer.setVvalue(1.0f);
                  }));
      textContainer.setContent(text);
      //....
  }
  
  public void appendBold(String msg) { //similar for italic and regular
      append(msg, "-fx-font-weight: bold");
  }
  
  private synchronized void append(String msg, String style) {
      Platform.runLater(() -&gt; {
          Text t = new Text(msg);
          t.setFont(segoe(13));
          if (!style.equals("")) {
              t.setStyle(style);
          }
          text.getChildren().add(t);
      });
  }
  </code></pre>
  
  <p>It won't win any awards for code style, but as this is a personal project I don't really care.</p>