I have the following layout in JavaFx 8 defined via .fxml:
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="730.0" prefWidth="1005.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<top>
<Pane fx:id="statusPane" prefHeight="30.0" prefWidth="1005.0" BorderPane.alignment="CENTER" />
</top>
<center>
<SplitPane fx:id="mainPane" dividerPositions="0.2" prefHeight="700.0" prefWidth="1005.0" minHeight="-Infinity" minWidth="-Infinity" BorderPane.alignment="CENTER">
<items>
<ScrollPane fitToHeight="true" fitToWidth="true" hbarPolicy="ALWAYS" vbarPolicy="ALWAYS" minHeight="-Infinity" minWidth="-Infinity">
<content>
<AnchorPane fx:id="menuPane" prefHeight="700.0" prefWidth="200.0" minHeight="-Infinity" minWidth="-Infinity" />
</content>
</ScrollPane>
<ScrollPane fitToHeight="true" fitToWidth="true" hbarPolicy="ALWAYS" vbarPolicy="ALWAYS" minHeight="-Infinity" minWidth="-Infinity">
<content>
<AnchorPane fx:id="formPane" prefHeight="700.0" prefWidth="800.0" minHeight="-Infinity" minWidth="-Infinity"/>
</content>
</ScrollPane>
</items>
</SplitPane>
</center>
</BorderPane>
I do not understand why SplitPane cannot get smaller than 1005x700 when user is resizing BorderPane during the runtime. SplitPane can get larger than 1005x700, which is fine, but it cannot get smaller than prefHeight and prefWidth, thought the Application window itself (BorderPane) can.
Why?
OK. I found the problem!
minHeight="-Infinity" minWidth="-Infinity"
generated by Scene Builder was a very bad idea!
The following code
minHeight="0" minWidth="0"
works as expected!
So do not use -Infinity
constant for the minHeight
or minWidth
. 0 is what you need!
The size of Layouts doesn't only depend on its size, but the size of the container(parent) / contents(children) as well
From Docs
A pane's parent will resize the pane within the pane's resizable range during layout.
The size of the SplitPane depends on its Parent. Since the Width
of BorderPane
is 1005.0 and Height
is 730.0 . The SplitPane fills the Parent(which it is supposed to)
Try removing the
prefHeight="730.0" prefWidth="1005.0"
from the BorderPane
in above fxml, you can find the SplitPane will decrease in height.
Why only height? It is because
<Pane fx:id="statusPane" prefHeight="30.0" prefWidth="1005.0" ..
forces the BorderPane to not decrease it's width.
Parent has to resize itself depending on the size of its child.
In short, if you are not sure how big or small your window can be, don't specify the prefHeight
or prefWidth
to your layouts
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.