1

Currently, I'm making a client-side anime streamer, and currently learning JavaFX. I've created a static thumbnail, and I need to change the value of the static content. So, how do you change the value of a text element already defined in a .fxml file in JavaFX?

For reference, here's the code.

videoThumbnail.fxml

<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.*?> <?import javafx.scene.image.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.text.*?> <AnchorPane prefHeight="387.0" prefWidth="243.0" style="-fx-background-color: #000000;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.gui.Controller"> <children> <ImageView fitHeight="266.0" fitWidth="184.0" layoutX="36.0" layoutY="26.0" pickOnBounds="true" preserveRatio="true"> <image> <Image url="@../images/mL0215_1024x1024.png" /> </image> </ImageView> <Label layoutX="36.0" layoutY="308.0" style="-fx-background-color: #000000;" text="My Hero Acadamia" textFill="WHITE"> <font> <Font name="System Bold" size="17.0" /> </font> </Label> </children> </AnchorPane> 

main.java

 @Override public void start(Stage primaryStage) throws Exception { try { FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/videoThumbnail.fxml")); Pane root = (Pane) loader.load(); Scene scene = new Scene(new Group(root)); primaryStage.setTitle("test"); primaryStage.setScene(scene); primaryStage.show(); letterbox(scene, root); primaryStage.setFullScreen(true); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { launch(args); } 

Controller.java

package test.gui; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.input.MouseEvent; public class Controller { } 
4
  • 2
    In the controller : @FXML Label yourLabel and in the fxml file : fx:id="yourLabel" then you can use the object to change the values Commented Sep 7, 2020 at 20:45
  • 1
    @BoHalim how would you do that? Commented Sep 7, 2020 at 20:48
  • 1
    First your Controller class needs implements Initializable Commented Sep 7, 2020 at 20:51
  • 4
    @BoHalim The controller does not need to implement Initializable. Commented Sep 7, 2020 at 22:48

1 Answer 1

4

First you need to assign an id attribute to the relevant tag. For example, if you wanted to reference the AnchorPane in the above FXML file, you would use this:

<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.*?> <?import javafx.scene.image.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.text.*?> <AnchorPane fx:id="anchorPane" prefHeight="387.0" prefWidth="243.0" style="-fx-background-color: #000000;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.gui.Controller"> ... </AnchorPane> 

Note the addition of fx:id="anchorPane". That is the id attribute.

Now, you can reference it from the Controller by using the id as the variable name. Just add the AnchorPane as an instance variable using the @FXML annotation.

package test.gui; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.input.MouseEvent; import javafx.scene.layout.AnchorPane; public class Controller { @FXML private AnchorPane anchorPane; /*Rest of class*/ } 

You can do this with any object in the FXML file, including the Labels. Just remember to add the id attribute.

Sign up to request clarification or add additional context in comments.

2 Comments

"You can do this with any Node in the FXML file..." – Not just any Node, but any Object.
@Slaw Good catch.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.