Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.4k views
Welcome To Ask or Share your Answers For Others

1 Answer

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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...