I have a VBox containing MFXToggleButtons. I want to collapse and expand this VBox in height so that it disappears completely. Having thus made a settings window that, when expanded, moves other objects on the form.
But when you try to specify any height in the VBox, nothing changes. As I understand it, this is because the objects inside the VBox do not allow it to decrease.
My VBox
<VBox>
<MFXToggleButton selected="true" text="Installed" >
<VBox.margin>
<Insets top="20" bottom="5" left="10" />
</VBox.margin>
</MFXToggleButton>
<MFXToggleButton selected="true" text="Releases">
<VBox.margin>
<Insets top="10" bottom="5" left="10" />
</VBox.margin>
</MFXToggleButton>
<MFXToggleButton selected="true" text="Modified">
<VBox.margin>
<Insets top="10" bottom="5" left="10" />
</VBox.margin>
</MFXToggleButton>
<MFXToggleButton selected="false" text="Snapshots">
<VBox.margin>
<Insets top="10" bottom="5" left="10" />
</VBox.margin>
</MFXToggleButton>
<MFXToggleButton selected="false" text="Olds">
<VBox.margin>
<Insets top="10" bottom="20" left="10" />
</VBox.margin>
</MFXToggleButton>
</VBox>
I have already tried to use other containers and also reduce the height of the MFXToggleButton themselves. But it didn't give me any results.
First note that the functionality you are describing is already implemented by
TitledPane. Typically you should use built-in functionality, when it does what you need, instead of trying to implement it yourself.If you want to write your own animation to collapse a pane, you can animate the value of the
maxHeightProperty(). To collapse it, you should animate the max height from the current height to zero. To expand it again, compute the preferred height usingprefHeight(width), and animate the max height from zero to the preferred height. At the end of the "expand" animation, you probably want to set the max height toRegion.USE_PREF_HEIGHT, so that it can continue to respect its preferred height if the contents change.A couple of implementation notes:
The default minimum height of a
VBoxis non-zero (see docs). To allow it to shrink completely, you need to explicitly set theminHeightto zero.Not all containers clip the content of child nodes if they overflow the bounds allocated to them. So you probably need to explicitly provide a clip and bind it to the bounds of the content.
You don't want the container for the expanding content to allocate extra space to it (because the algorithm described above assumes it is not taller than its preferred size). You can guarantee this (at least in cases where it's possible) by setting the
maxHeighttoRegion.USE_PREF_SIZE.Here is a quick example:
hello-view.fxml:HelloController.java:HelloApplication.java:Expanded:
Collapsed: