Dynamically assign Controller to JavaFX-fxml

It's a story when there are rare cases where you want to dynamically switch controller processing (event handling, etc.) while sharing fxml.

Implementation

common.fxml

javafx.jpg

--Do not assign a controller in fxml

HogeController


package sample.javafx;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

import java.net.URL;
import java.util.ResourceBundle;

public class HogeController implements Initializable {
    @FXML
    private Label label;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        this.label.setText("Hoge");
    }
}

Main.java


package sample.javafx;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/common.fxml"));
        
        loader.setController(new HogeController());
        
        Parent root = loader.load();
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

Execution result javafx.jpg

Implementation 2

FugaController.java


package sample.javafx;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

import java.net.URL;
import java.util.ResourceBundle;

public class FugaController implements Initializable {
    @FXML
    private Label label;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        this.label.setText("Fuga");
    }
}

Main.java


package sample.javafx;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/common.fxml"));
        
        loader.setController(new FugaController()); //★ Change controller
        
        Parent root = loader.load();
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

Execution result

javafx.jpg

Description

Main.java


        FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/common.fxml"));
        
        loader.setController(new FugaController()); //★ Change controller
        
        Parent root = loader.load();

--Instantiate FXMLLoader with new and assign any controller instance with setController () before load ()

Recommended Posts

Dynamically assign Controller to JavaFX-fxml
Introduction to RSpec 5. Controller specs
Assign evaluation result to Java variable