A GridPane
has one or more columns. Each column can have constraints placed upon it, which is accomplished via ColumnConstraints
instances added to the GridPane#getColumnConstraints()
list. From the documentation:
Returns list of column constraints. Column constraints can be added to explicitly control individual column sizing and layout behavior. If not set, column sizing and layout behavior is computed based on content. Index in the ObservableList denotes the column number, so the column constraint for the first column is at the position of 0 [emphasis added].
Note: If you are not aware, FXML file simply describes an object graph and then an FXMLLoader
parsers the file and sets the appropriate properties or adds elements to the appropriate collections. The <columnConstraints>
element references the list mentioned above and the ColumnConstraints
instances are added to said list. See Introduction to FXML for more information.
As you can see, the index of the ColumnConstraints
in the list determines which column the constraints apply to. Your FXML file adds two constraints, which means you're adding constraints for both column one and column two. Though, to be fair, it doesn't make sense for two columns to both consume 100% of the width. Now, if your sample.fxml
file was created as you've posted it (i.e. you didn't change much) and you're wondering why your tool automatically added two constraints—that I can't answer.
Note there's no requirement that each index requires a unique ColumnConstraints
instance. In other words, you could use the same instance for multiple columns by adding that instance to the list multiple times. In FXML that would look like:
<GridPane fx:controller="sample.Controller" xmlns:fx="http://javafx.com/fxml"
alignment="center" hgap="10" vgap="10" gridLinesVisible="true">
<fx:define>
<ColumnConstraints fx:id="colConstraints" percentWidth="100"/>
</fx:define>
<columnConstraints>
<fx:reference source="colConstraints"/>
<fx:reference source="colConstraints"/>
</columnConstraints>
</GridPane>
As for why your view changed when you removed one of the ColumnConstraints
, the presence of a constraints forces the column to exist even if there's no content in the column. And when there's no content in the column, if you remove the constraint the column will cease to exist.
The behavior of RowConstraints
is the same, except the constraints apply to the rows instead of the columns (obviously).