7

I have Label on my fxml file:

 <children>
    <Label fx:id="lblTest" text="Label" />
 </children>

How can i change the text from "Label" to "Hello" from the main/controller java file?

I just started to learn the basics of JavaFX and i am not sure if it possible

Oshrib
  • 1,789
  • 12
  • 40
  • 67
  • 1
    Add a member `@FXML Label lblTest;` to your Controller, from there you will be able to change it via `lblTest.setText()`. – aw-think Sep 27 '15 at 08:45
  • @NwDx thanks. but it not so simple, right? i need to create method for this? i can't just put lblText.setText() there like other language code like asp.net with label on c# file,with the FXML i need to do "getConroler()" or something else that my logic says – Oshrib Sep 27 '15 at 08:53

2 Answers2

15

Problem

You want to set the text on a label that is part of FXML/Controller/MainApp

Solution

Normally you have three files:

  1. FXML-Document
  2. FXML-Controller
  3. Class that extends Application and overrides the start method

A little Example:

LabelText.java

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

public class LabelText extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

FXMLDocument.fxml

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.40" fx:controller="labeltext.FXMLDocumentController">
    <children>
        <Label fx:id="lblTest" layoutX="126.0" layoutY="92.0" minHeight="16" minWidth="69" />
    </children>
</AnchorPane>

FXMLDocumentController.java

package labeltext;

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

public class FXMLDocumentController {

    @FXML
    private Label lblTest;

    @FXML
    private void initialize() {
        lblTest.setText("I'm a Label.");
    }
}

And that's it.

aw-think
  • 4,723
  • 2
  • 21
  • 42
  • 1
    Exactly what i looked for. also, i found other answer and i posted it here, but i will take your solution. Thanks ! – Oshrib Sep 27 '15 at 09:15
0
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Label lblData = (Label) root.lookup("#lblTest");
if (lblData!=null) lblData.setText("bye");
Oshrib
  • 1,789
  • 12
  • 40
  • 67
  • 1
    Your attempt probably break the MVC Pattern of JavaFX. – aw-think Sep 27 '15 at 09:08
  • This will not work the way you have written it. Lookups only work after CSS has been applied, which is usually the first time the label is rendered to the screen. – James_D Sep 27 '15 at 12:36