1

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

Isaí
  • 49
  • 3
  • 2
    *”I don’t know what’s going on”*. Actually, you have figured out *exactly* what’s going on. `FXMLLoader.load()` creates a new instance of the controller (as well as a new instance of the UI defined in the FXML file). I’m away from my computer right now, but search for “nested controllers” to see how to get a reference to the controller that is created when you include the FXML file. – James_D Oct 20 '22 at 15:50
  • See, for example, https://stackoverflow.com/questions/44467982/javafx8-fxml-naming-of-nested-controllers – James_D Oct 20 '22 at 15:54
  • @James_D one more question, is that the best way to achieve communication between controllers? I had thought using zeromq, but i think it would complicate the code – Isaí Oct 20 '22 at 16:12
  • 2
    Don't communicate between controllers directly (the "nested controller" case may be an exception.) Use a [MVC approach](https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx) – James_D Oct 20 '22 at 16:34

0 Answers0