2

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...

1

1 Answer 1

7

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);
    }
}
3
  • thanks... I will just create a Pane and will add labels to it separately. Feb 18, 2015 at 17:11
  • Also, to print numbers from 1 to 1000 just write this code: sout(1); sout(2);...sout(1000); .. Javafx is so raw.
    – Stepan
    Oct 2, 2017 at 21:44
  • @Stepan I'm not sure I understand your point. I explained at the beginning of the answer how you would do this in JavaFX if you didn't want to write code for each label individually. The question explicitly asks how to put FXML elements into a list, and since there's no looping construct in XML the only way to do it in FXML is by defining each one directly. There are perhaps some use cases where this would be an appropriate approach, even if it is not scalable.
    – James_D
    Oct 2, 2017 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.