Jonas Ricke
Jonas Ricke

Reputation: 51

JavaFx setText for Label

i'm new at JavaFx and actually i'm trying to change/set a label text. In my eyes i did everything that is to do but it does not work. Hope that someone could help me. I'm searching now for hours but guess i'll never find my misstake. Furthermore i guess the misstake is to find at the controller but in some examples i watched, it looks similar to my code..

The FXML:

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<SplitPane id="Split" dividerPositions="0.3" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="620.0" prefWidth="871.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="presentation.DataController">
  <items>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
         <children>
            <Label id ="name" fx:id="name" layoutX="26.0" layoutY="21.0" prefHeight="576.0" prefWidth="205.0" textAlignment="CENTER"/>
         </children></AnchorPane>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
         <children>
            <Label fx:id="daten" layoutX="19.0" layoutY="14.0" prefHeight="576.0" prefWidth="511.0"/>
 <!--           <Slider layoutX="573.0" layoutY="11.0" orientation="VERTICAL" prefHeight="576.0" prefWidth="14.0" /> !-->
         </children></AnchorPane>
  </items>
</SplitPane>

My Controller:

package presentation;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

public class DataController implements Initializable {

    @FXML
    private Label name;
    private Label daten;

    private void init() {
        name.setText("Hello World!");
        daten.setText("AnotherTest");
    }


    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

}

And the Application Class

package presentation;


import application.*;
import data.*;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;


public class Data extends Application {

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

        stage.setTitle("Übung 0");
        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

Upvotes: 1

Views: 19190

Answers (2)

Ahmet Gokpinar
Ahmet Gokpinar

Reputation: 11

You need to load your fxml file in the controller class. Something like this:

Parent warningRoot = FXMLLoader.load(getClass().getResource("yourfxmlfile.fxml"));
Scene scene = new Scene(warningRoot);
Stage stage = new Stage();

Upvotes: 0

mr mcwolf
mr mcwolf

Reputation: 2859

public class DataController implements Initializable {

    @FXML
    private Label name;

    @FXML
    private Label daten;

    private void init() {
        name.setText("Hello World!");
        daten.setText("AnotherTest");
    }


    @Override
    public void initialize(URL url, ResourceBundle rb) {
        init();
    }    

}

Upvotes: 2

Related Questions