title: JavaFX : Adapt component height to its children height?
body: <p>I'm trying to develop a desktop app using javaFX (2.2), and I'm experiencing some difficulties to really get it to behave like I would like it to. Being a new user of the framework, I might not be using it as it should be...</p>
  
  <p>What I want is a kind of dashbord, with several JavaFX or custom controls inside a ScrollPane. I currently have several views that look like this :</p>
  
  <pre><code>&lt;AnchorPane id="userContent" xmlns:fx="http://javafx.com/fxml" fx:controller="squalp.view.OrgViewController"&gt;
      &lt;children&gt;
          &lt;ScrollPane fx:id="scrollPane" styleClass="transparent" hbarPolicy="NEVER" vbarPolicy="AS_NEEDED" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"&gt;
              &lt;content&gt;
                  &lt;VBox fx:id="vBox" styleClass="transparent" alignment="TOP_LEFT" focusTraversable="true"&gt;
                      &lt;children&gt;
                          &lt;Label text="Label" /&gt;
                          &lt;Button fx:id="bTest" text="Test" onAction="#test"/&gt;
                          &lt;TreeView fx:id="orgTree" focusTraversable="true" /&gt;
                      &lt;/children&gt;
                  &lt;/VBox&gt;
              &lt;/content&gt;
          &lt;/ScrollPane&gt;
      &lt;/children&gt;     
  &lt;/AnchorPane&gt;
  </code></pre>
  
  <p>As long as I keep using simple controls, no problem, items are correctly placed one after another in th VBox.</p>
  
  <p>But when using TreeViews or TableViews, I can only get my controls to have fixed height, meaning extra space on the bottom, or new scroll bars, in addition to the main one. What I would like to achieve is to adapt my controls to their current content height.</p>
  
  <p>Qt's QML, for exemple, has a childrenHeight property, and it's kind of what I'm looking for. I figure this content height must be calculated somehow, since components scrollbars do adapt to the content.</p>
  
  <p>I had to use a workaround for tableviews :</p>
  
  <pre><code>table.setPrefHeight(list.size() * rowHeight + headerHeight);
  </code></pre>
  
  <p>but I couldn't find a way to track displayed items on a treeview.</p>
  
  <p>Has anyone an idea of what I could be doing to handle this issue ? Or I am really not using the framework as I should ?</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>EDIT</strong> : Visual description of the problem :</p>
  
  <p><a href="http://postimg.org/image/6dlysp5v3/" rel="nofollow">Case 1</a></p>
  
  <p>Here my treeview's height is set to a default value. There are not enough items to fill the treeview : if I want to put another component below, there'll be a gap; if my scene is not big enough, the ScrollPane's scrollbar appears, even though all the displayable content is visible.</p>
  
  <p><a href="http://postimg.org/image/y6te35c99/" rel="nofollow">Case 2</a></p>
  
  <p>If if expand my treeview's items, there are now too many of them. They are not all visible at once, and the treeview's scrollbar appears, even though I already have a scrollable view with the scrollpane.</p>
  
  <p>What I would like is my TreeView to adapt to its content height (reduced height if items are collapsed, bigger height if they are expanded), in order to be able to put several components one after another and to scroll through them all using the ScrollPane's scrollbar.</p>
  
  <p>Since the TreeView component has itself a scrollbar, its visible content's height must be calculated at some point (since scrollbars basically use ratios based on contentHeight/componentHeight). This information doesn't seem to be available in the public API, but I wonder if, developping a custom class that expands TreeView, there could be a way to bind my component to its "visible height".</p>