I would have expected that a GridPane calculates each column's default width according to the maximum preferred width of all elements in that column. However, in my code it seems to calculate a too small width, resulting in one of my labels in the column being cut off.
Here's my "cashflowform.fxml":
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.shape.Box?>
<GridPane xmlns:fx="http://javafx.com/fxml" fx:controller="finfx.ui.CashFlowForm"
style="-fx-padding:25;"
vgap="5" hgap="5">
<Label text="Datum"
GridPane.columnIndex="0" GridPane.rowIndex="0">
</Label>
<DatePicker fx:id="datePicker" GridPane.columnIndex="1" GridPane.rowIndex="0"/>
<Label text="Betrag"
GridPane.columnIndex="0" GridPane.rowIndex="1">
</Label>
<TextField GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<Label text="Kategorie"
GridPane.columnIndex="0" GridPane.rowIndex="2">
</Label>
<TextField GridPane.columnIndex="1" GridPane.rowIndex="2"/>
<Label text="Anmerkung"
GridPane.columnIndex="0" GridPane.rowIndex="3">
</Label>
<TextField GridPane.columnIndex="1" GridPane.rowIndex="3"/>
<Box GridPane.columnIndex="0" GridPane.rowIndex="4"
GridPane.columnSpan="2"
GridPane.hgrow="ALWAYS"
height="20">
</Box>
<Button text="Speichern"
GridPane.columnIndex="0" GridPane.rowIndex="5"
GridPane.columnSpan="2"
GridPane.hgrow="ALWAYS" maxWidth="Infinity">
</Button>
<Button text="Speichern & Weitere Position..."
GridPane.columnIndex="0" GridPane.rowIndex="6"
GridPane.columnSpan="2"
GridPane.hgrow="ALWAYS" maxWidth="Infinity">
</Button>
<Button text="Abbrechen"
GridPane.columnIndex="0" GridPane.rowIndex="7"
GridPane.columnSpan="2"
GridPane.hgrow="ALWAYS" maxWidth="Infinity">
</Button>
</GridPane>
And this is how I set up the stage (note I'm not setting any size, so I let it calculate the size itself):
primaryStage.setScene(new Scene(FXMLLoader.loadFxmlRoot("cashflowform")));
primaryStage.show();
Result:
As you can see the first column is too small to fully show my "Anmerkung" label. Why is that, and what is the best way to fix this?
PS: As a side-question, if anyone cares, I also noted that my "Box" that I added as a space filler between my labels/fields and buttons is actually displayed as a little white vertical line, when you look closely. It seems a Box is not the right thing to use for this. I'm just starting with JavaFX, what would be the appropriate counterpart to Swing's "Box.createVerticalGlue()" ?
PPS: Before anyone asks: The "FXMLLoader" seen in my code is not the javafx.fxml.FXMLLoader, but a custom utility class
Thank you!
I found it out. Setting
minWidth="-Infinity"
for the label fixes it, because -Infinity actually resolves to Region/Control.USE_PREF_SIZE.
I don't know why it is neccessary to do this, because using the preffered size should be the default behaviour of column sizing according to GridPane's documentation. It seems as some kind of hack to me to have to do it like this. But anyways, this solves the problem.