Changing Label text JavaFX FXML

18,584

Just remove statics for field and method, then run your application by main() in Main class:

public class MainController {
    @FXML
    private Label aaa;

    @FXML
    public void initialize(){
        aaa.setText("AHOJ");
    }
}
Share:
18,584
M. Barabas
Author by

M. Barabas

Updated on June 13, 2022

Comments

  • M. Barabas
    M. Barabas almost 2 years

    There is my main class:

    public class Main extends Application {
    private static Stage primaryStage;
    public static BorderPane mainLayout;
    
    @Override
    public void start(Stage primaryStage) {
        this.setPrimaryStage(primaryStage);
        primaryStage.setTitle("Project");
    
        try {
            mainLayout = 
            FXMLLoader.load(Main.class.getResource("/main/view/MainPage.fxml"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            Scene scene = new Scene(mainLayout);
            primaryStage.setScene(scene);
            primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
    
                @Override
                public void handle(WindowEvent event) {
                    System.exit(0);
                }
            });
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    
        public static Stage getPrimaryStage() {
            return primaryStage;
        }
    
        public void setPrimaryStage(Stage primaryStage) {
             Main.primaryStage = primaryStage;
        }
    }
    

    This FXML of my window:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.geometry.Insets?>
    <?import javafx.scene.control.Label?>
    <?import javafx.scene.layout.BorderPane?>
    <?import javafx.scene.layout.VBox?>
    <?import javafx.scene.text.Font?>
    
    <BorderPane prefHeight="410.0" prefWidth="512.0" 
    xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" 
    fx:controller="main.controller.MainController">
     <center>
      <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" 
      spacing="20.0" BorderPane.alignment="CENTER">
         <children>
            <Label fx:id="aaa" prefHeight="72.0" prefWidth="336.0" 
    text="Project" textAlignment="CENTER">
               <font>
                  <Font name="Century Gothic" size="25.0" />
               </font>
            </Label>
         </children>
         <padding>
            <Insets bottom="30.0" />
         </padding>
       </VBox>
     </center>
    </BorderPane>
    

    This is Controller for this FXML:

    public class MainController {
    @FXML
    private static Label aaa;
    
    @FXML
    public static void initialize(){
        aaa.setText("AHOJ");
    
        }
    } 
    

    I want to call method initialize() from another class ten times like this:

    public class MyClass {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            MainController.initialize();
        }
    }
    }
    

    But there is NullPointerException. Can someone help me?

    • Dmytro Maslenko
      Dmytro Maslenko almost 7 years
      You got NPE because your static field is not initialized. What is the final goal of your for loop?
    • M. Barabas
      M. Barabas almost 7 years
      It is example. My goal is to update Label text more then just one time by calling method from another class but I dont know how.
    • Dmytro Maslenko
      Dmytro Maslenko almost 7 years
      What is other class? This is a FX application, you run via main() method and it runs in separate thread. From which class are you planning to change a label?
    • M. Barabas
      M. Barabas almost 7 years
      From class MyClass as it is in example. I need call some method or something to change label text.
  • M. Barabas
    M. Barabas almost 7 years
    I know that it works, but I want to call method initialize() from another class. Or I want to find any other way, how to update my Label text from another class more times. Every time other word for example.
  • Dmytro Maslenko
    Dmytro Maslenko almost 7 years
    It is impossible to run both main() from Main and from MyClass and interact between each other. You may change a label only from FX application as reaction on user action or from background job. Say, an user does something and as result a label changed.
  • purring pigeon
    purring pigeon almost 7 years
    Your controller should be used to update or "control" the items on the FXML that they represent. If you want to invoke an update from somewhere else you can look at links on how to do just that. Create a method on the controller... stackoverflow.com/questions/33237356/…