My problem is: I want to change something from the .fxml but whatever I do, nothing changes. This is just a simply example.
I went trough the whole internet but none of the solutions worked for me. Here I want to change the text of the label, by calling the corresponding method from the main class.
Calling the same method (here setLabel()) when clicking a Button, with an event handler in the controller class, everything works fine, but a soon as I try to modify something from another class nothing works.
Main class:
package sample; import javafx.application.Application; import javafx.application.Platform; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); primaryStage.setTitle("Hello World"); primaryStage.setScene(new Scene(root, 300, 275)); primaryStage.show(); Controller controller = new Controller(); Platform.runLater(()->controller.setLabel()); } FXML Code:
<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"> <center> <Label fx:id="label" text="This" BorderPane.alignment="CENTER" /> </center> </BorderPane> Controller class: package sample;
import javafx.fxml.FXML; import javafx.scene.control.Label; public class Controller { @FXML private Label label=new Label(); public void setLabel(){ label.setText("Test"); } }