Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javafx

fxml

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

like image 707
Paul Avatar asked Aug 16 '17 08:08

Paul


People also ask

How do I change a label in JavaFX?

You can change the text of a label using its setText() method. This can be done while the application is running. Here is an example of setting the text of a JavaFX Label: label.

How do you change the size of a label in JavaFX?

Label Font To set a font text size, use the setFont method from the Label class. The following code sets the size of the label1 text to 30 points and the font name to Arial. label. setFont(new Font("Arial", 30));

What is the difference between label and text in JavaFX?

A Text is a geometric shape (like a Rectangle or a Circle), while Label is a UI control (like a Button or a CheckBox). In Swing, geometric shapes were restricted to the painting mechanism, while in JavaFX they can be used in more generic ways. And you can use a Text clipping, giving the node shape by the text.


1 Answers

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>
like image 86
Puce Avatar answered Oct 02 '22 13:10

Puce