Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FXMLLoader how to access the components by FXID?

I'm trying to figure out how to work with JavaFx. I built the application interface in Scene Builder. But I can not get access to the component, since all loaded into the Parent.

Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

If I change "Parent" on "Pane" I can get access to the getChildren(), but it is not clear how to get control if i know fx:id...

The question is even simpler. I added Label or TextField in Scene Builder. How do I change it's text from the code if I know the fx:id?

I am in despair.

like image 469
JavaPanic Avatar asked Nov 16 '14 22:11

JavaPanic


People also ask

What is@ FXML annotation?

The @FXML annotation enables an FXMLLoader to inject values defined in an FXML file into references in the controller class.

How to specify the controller in FXML file?

It is also possible to assign the controller class directly from the FXML file. In order to do that, you should first open your FXML file and add the following code on the first line of the file right after declarations. Example: Since my controller is inside the package name “view”, my fx: controller = “view.

What is an FXML file?

FXML is an XML-based user interface markup language created by Oracle Corporation for defining the user interface of a JavaFX application. FXML presents an alternative to designing user interfaces using procedural code, and allows for abstracting program design from program logic.

How to Add controller in FXML JavaFX?

An application's View should be linked with its Controller inside the start() method of the JavaFX application. Links should be created by storing a reference to the Java controller inside the FXML file. This is then automatically linked when the FXMLLoader creates the View.


2 Answers

FXMLLoader.getNamespace() can be used, this is a map of named components.

FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
Parent root = loader.load();
TextField foo = (TextField)loader.getNamespace().get("exampleFxId");
like image 142
Adam Avatar answered Sep 21 '22 11:09

Adam


You should create a controller class for your FXML document, in which you can perform any functionality you need to perform involving the UI components. You can annotate fields in that class with @FXML and they will be populated by the FXMLLoader, matching the fx:id attribute to the field name.

Work through the tutorial for more details, and have a look at the Introduction to FXML documentation.

Simple example:

Sample.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Button?>

<VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="SampleController">
    <Label fx:id="countLabel"/>
    <Button fx:id="incrementButton" text="Increment" onAction="#increment"/>
</VBox>

SampleController.java:

import javafx.fxml.FXML;
import javafx.scene.control.Label;


public class SampleController {

    private int count = 0 ;

    @FXML
    private Label countLabel ;

    @FXML
    private void increment() {
        count++;
        countLabel.setText("Count: "+count);
    }
}

SampleMain.java:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class SampleMain extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Scene scene = new Scene(FXMLLoader.load(getClass().getResource("Sample.fxml")), 250, 75);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
like image 34
James_D Avatar answered Sep 23 '22 11:09

James_D