im working with JavaFX and i need modify ui components of a second controller in a main controller, so i have the next structure
├── java
│ ├── com
│ │ └── example
│ │ └── demo2
│ │ ├── ExampleController.java
│ │ ├── MainApplication.java
│ │ └── MainController.java
│ └── module-info.java
└── resources
└── com
└── example
└── demo2
├── example.fxml
└── main.fxml
and i've tried this but its not working
- MainApplication.java
package com.example.demo2;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class MainApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(MainApplication.class.getResource("main.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 320, 240);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
- MainController.java
package com.example.demo2;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class MainController implements Initializable {
@FXML
protected void onHelloButtonClick() {
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("example.fxml"));
try {
loader.load();
ExampleController controller = loader.getController();
controller.welcomeText.setText("HELLO WORLD");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
- ExampleController.java
package com.example.demo2;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import java.net.URL;
import java.util.ResourceBundle;
public class ExampleController implements Initializable {
@FXML
public Label welcomeText;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
System.out.println(this);
}
}
- example.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<HBox prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.demo2.ExampleController">
<Label fx:id="welcomeText" text="example" />
</HBox>
- main.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Button?>
<VBox alignment="CENTER" spacing="20.0" xmlns:fx="http://javafx.com/fxml"
fx:controller="com.example.demo2.MainController">
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/>
</padding>
<fx:include source="example.fxml"/>
<Button text="Hello!" onAction="#onHelloButtonClick"/>
</VBox>
i've noticed when i do loader.load();
that another controller is created, so i've modified that controller which is not the same that the one is created when the application is launching, i get this output when run my program
com.example.demo2.ExampleController@4c62adb4
com.example.demo2.ExampleController@5b74a187
i dont know whats going on, if anybody can help me, i would appreciate it