create array of Label using FXML in JavaFX

11,584

Some things are just better done in Java than FXML. I would not create the labels in FXML at all in this scenario; just create the pane that is going to hold them, then create the labels in the controller and add them to the pane.

That said, it can be done the way you are asking, using <fx:reference>.

Do something like this:

<!-- create all the labels as usual -->
<Label fx:id="label1" text="Message 1"/>
<Label fx:id="label2" text="Message 2"/>
<!-- ... -->

<!-- use a define block to define an array list containing the labels: -->
<fx:define>
    <ArrayList fx:id="labelList">
        <fx:reference source="label1" />
        <fx:reference source="label2" /> 
        <!-- ... --> 
    </ArrayList>
</fx:define>

Then in the controller just inject the list:

@FXML
private List<Label> labelList ;

Complete example:

LabelListTest.fxml:

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

<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>
<?import java.util.ArrayList?>

<VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="LabelListController">
    <Label  fx:id="label1"/>
    <Label  fx:id="label2"/>
    <Label  fx:id="label3"/>
    <Label  fx:id="label4"/>
    <Label  fx:id="label5"/>
    <Label  fx:id="label6"/>
    <Label  fx:id="label7"/>
    <Label  fx:id="label8"/>
    <Label  fx:id="label9"/>
    <Label  fx:id="label10"/>

    <fx:define>
        <ArrayList fx:id="labelList" >
            <fx:reference source="label1"/>
            <fx:reference source="label2"/>
            <fx:reference source="label3"/>
            <fx:reference source="label4"/>
            <fx:reference source="label5"/>
            <fx:reference source="label6"/>
            <fx:reference source="label7"/>
            <fx:reference source="label8"/>
            <fx:reference source="label9"/>
            <fx:reference source="label10"/>
        </ArrayList>
    </fx:define>
</VBox>

LabelListController.java:

import java.util.List;

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


public class LabelListController {
    @FXML
    private List<Label> labelList ;

    public void initialize() {
        int count = 1 ;
        for (Label label : labelList) {
            label.setText("Message " + (count++) );
        }
    }
}

LabelListTest.java:

import java.io.IOException;

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

public class LabelListTest extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("LabelListTest.fxml"));
        Scene scene = new Scene(root, 250, 450);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Share:
11,584
Ashish Chauhan
Author by

Ashish Chauhan

Updated on July 12, 2022

Comments

  • Ashish Chauhan
    Ashish Chauhan almost 2 years

    Actually, I want to create a navigation list using Labels in Javafx. I can assign fx:id to each label and create labels in controller class.

    But what I want to do is, instead of ten Label objects in controller class, I want an array of Labels in controller class, which I created in scene builder.

    Can somebody help me figure out a way...