I have a FXML file with all the elements like Button
s, Label
s, TextField
s etc. For the FXML-file I have a controller class, where I can set the text of Label
s and so on.
Now I have an extern class, where I need to change the Button
s, Label
s etc. too. The problem is, I can't set the elements to static
. So how can I change them in a different class?
I already know that there are similiar questions here, but nothing what really helped me. I would really appreciate your help.
Example
FXML
<AnchorPane fx:id="root" prefHeight="1000" prefWidth="1000"
xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="Controller">
<Label fx:id="testLabel" layoutX="150" layoutY="200" text=""/>
Controller.java
@FXML private Label testLabel;
AnotherClass.java
testLabel.setText("This is a test");
Main.java
Main.primaryStage = primaryStage;
primaryStage.setTitle("Test Scene");
AnchorPane pane = FXMLLoader.load(Resource.onClassPath("MyScene.fxml").getURL());
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
If I am in the Scene "MyScene.fxml" and click a Button to navigate to the next Scene
@FXML AnchorPane root;
AnchorPane pane = FXMLLoader.load(Resource.onClassPath("Example.fxml").getURL());
root.getChildren().setAll(pane);
Now the "Example" Scene has a Controller. I need to get access to the Elements of the Controller from a third Class.. :)