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");
}
}
Author by
M. Barabas
Updated on June 13, 2022Comments
-
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 almost 7 yearsYou got NPE because your static field is not initialized. What is the final goal of your for loop?
-
M. Barabas almost 7 yearsIt 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 almost 7 yearsWhat 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 almost 7 yearsFrom class
MyClass
as it is in example. I need call some method or something to change label text.
-
-
M. Barabas almost 7 yearsI 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 almost 7 yearsIt 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 almost 7 yearsYour 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/…