LoginSignup
5
5

More than 5 years have passed since last update.

JavaFX - fxml に Controller を動的に割り当てる

Posted at

fxml は共通にしつつ、コントローラ処理(イベントハンドリングとか)を動的に切り替えたいみたいなことが、まれにあったりなかったりしたときの話。

実装

common.fxml

javafx.jpg

  • 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();
    }
}

実行結果
javafx.jpg

実装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()); // ★コントローラを変更

        Parent root = loader.load();
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

実行結果

javafx.jpg

説明

Main.java
        FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/common.fxml"));

        loader.setController(new FugaController()); // ★コントローラを変更

        Parent root = loader.load();
  • FXMLLoadernew でインスタンス生成して、load() する前setController() で任意のコントローラインスタンスを割り当てる
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5