JavaFx: How to set the labelFor property of a Label in FXML?
Asked Answered
L

3

5

I have a TextField and a Label with mnemonicParsing true. I want to set the labelFor property of the label with the id of the TextField. How do I do that in FXML?

<GridPane hgap="5.0" snapToPixel="false" vgap="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
      <Label maxWidth="-Infinity" text="_A Label" GridPane.halignment="RIGHT" GridPane.hgrow="NEVER" GridPane.vgrow="NEVER" mnemonicParsing="true" />
      <TextField id="myTextField" GridPane.columnIndex="1" GridPane.hgrow="NEVER" />
</children>
</GridPane>

Thanks

Labuan answered 16/8, 2017 at 8:47 Comment(0)
D
6

You can use the dollar notation:

<GridPane hgap="5.0" snapToPixel="false" vgap="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
    <children>
          <Label labelFor="$myTextField" maxWidth="-Infinity" text="_A Label" GridPane.halignment="RIGHT" GridPane.hgrow="NEVER" GridPane.vgrow="NEVER" mnemonicParsing="true"  />
          <TextField id="myTextField" GridPane.columnIndex="1" GridPane.hgrow="NEVER" />
    </children>
</GridPane>
Deerhound answered 29/8, 2017 at 14:4 Comment(2)
I think id="myTextField" should be fx:id="myTextField" hereCavanagh
I found that for me this only works if the referenced node (TextField) is declared before the label that references it in the FXML file.Clasping
L
4

If found the solution...

<GridPane hgap="5.0" snapToPixel="false" vgap="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
      <Label maxWidth="-Infinity" text="_A Label" GridPane.halignment="RIGHT" GridPane.hgrow="NEVER" GridPane.vgrow="NEVER" mnemonicParsing="true">
           <labelFor>
               <TextField id="myTextField" GridPane.columnIndex="1" GridPane.hgrow="NEVER" />
           </labelFor>
      </Label>
      <fx:reference source="myTextField" />
</children>
</GridPane>
Labuan answered 16/8, 2017 at 16:39 Comment(0)
H
0

It's long time ago that this is edited, but the only correct solution for this is the following:

<Label labelFor="${myTextField}" ... />
<TextField fx:id="myTextField" ... />

The reference-id has to be enclosed in curly brackets and the id of the referenced control is the fx:id.

Herwig answered 15/5 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.