JavaFXML从所有类对Controller的引用 [英] JavaFXML reference to Controller from all classes

查看:102
本文介绍了JavaFXML从所有类对Controller的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的MainView类,它是启动整个程序的类.

I have my MainView class, which is the one that starts up the whole program.

public class MainView extends Application {
@Override
public void start(Stage stage) throws IOException {
    stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
        @Override
        public void handle(WindowEvent t) {
            Platform.exit();
            System.exit(0);
        }
    });

    FXMLLoader loader = new FXMLLoader(getClass().getResource("NavigationView.fxml"));
    Parent root = loader.load();
    Scene scene = new Scene(root);
    stage.setResizable(false);
    stage.setScene(scene);
    stage.show();
    stage.setTitle("Greenhouse");
}

/**
 * @param args the command line arguments
 * @throws java.sql.SQLException
 */
public static void main(String[] args) throws SQLException {
    launch(args);
}

}

该类加载我的FXML并启动您可能知道的程序.控制器在FXML内部指定.现在,我想要的是能够从程序中的任何类对该控制器进行引用.这是因为我希望每个类中的所有System.out.prints都打印到控制器中的TextArea中.这是我的控制器中的TextArea:

This class loads my FXML and starts the program as you probably know. The controller is specified inside the FXML. Now what I want, is to be able to make a reference to this controller from any class in my program. That is because I want all my System.out.prints in every class to print out to my TextArea that is in my controller. This is the TextArea in my controller:

@FXML
private TextArea GUIprint;

所以我的问题是,如何正确引用我的控制器,以便可以在所有类中使用它?我知道只要在其他类中创建控制器的实例就可以给我NullPointerException.

So my question is, how do I make the right reference to my controller, so I can use it in all classes? I know that just making an instance of the controller in other classes would just give me a NullPointerException.

如果您需要在控制器中查看我的Initialize方法,就在这里,它只是告诉启动时哪些窗格可见:

If you need to see my Initialize method in my controller, here it is, it just tells what pane to be visible at startup:

@Override
public void initialize(URL url, ResourceBundle rb) {
    loginPane.setVisible(true);

}

推荐答案

您的问题非常不清楚,因为您指的是其他类",但绝对没有指出这些类是什么,或在何处实例化它们.无论如何,我将尽力回答,涵盖所有可能性.

You question is very unclear, because you refer to "other classes" but give absolutely no indication as to what those classes are, or where you instantiate them. I will try to answer anyway, covering all possibilities.

由于start()方法是整个应用程序的入口,因此,可以创建任何其他对象的唯一位置是:

Since the start() method is the entry point to the entire application, the only places you can be creating any other objects are:

  1. 在控制器本身中,在initialize()方法中或在事件处理程序中
  2. start()方法中
  3. 从您从1.或2.创建的对象中,或从由这些对象创建的对象中,依此类推.
  1. In the controller itself, either in the initialize() method, or in an event handler
  2. In the start() method
  3. From objects you create from 1. or 2., or from objects you create from those, etc.

如果您要在控制器本身中创建其他对象,则只需传递对它们的引用,例如

If you are creating those other objects in the controller itself, then you just pass a reference to them, e.g.

@FXML
private void someHandlerMethod(ActionEvent event) {
    SomeOtherClass someObject = new SomeOtherClass();
    someObject.setController(this);
    // ...
}

如果要在start()方法中创建其他对象,则可以从FXMLLoader获取控制器实例,并将其传递给其他对象:

If you are creating those other objects in the start() method, you can get the controller instance from the FXMLLoader, and pass it to the other objects:

public void start(Stage stage) throws IOException {
    stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
        @Override
        public void handle(WindowEvent t) {
            Platform.exit();
            System.exit(0);
        }
    });

    FXMLLoader loader = new FXMLLoader(getClass().getResource("NavigationView.fxml"));
    Parent root = loader.load();

    MyController controller = loader.getController();
    SomeOtherClass someOtherObject = new SomeOtherClass();
    someOtherObject.setController(controller);

    Scene scene = new Scene(root);
    stage.setResizable(false);
    stage.setScene(scene);
    stage.show();
    stage.setTitle("Greenhouse");
}

在任何一种情况下,只需在其他类中定义适当的setter方法即可:

In either case, just define the appropriate setter methods in the other class(es):

public class SomeOtherClass {

    private MyController controller ;

    public void setController(MyController controller) {
        this.controller = controller ;
    }

    // ...
}

在第三种情况下,您可以递归地执行相同的操作;将控制器引用传递给一个对象,然后将其从该对象传递给需要它的人,等等.

In the third case, you can just recursively do the same thing; pass the controller reference to one object, and then pass it from that object to whoever needs it, etc.

更干净的方法可能是使用MVC方法,并与所有需要修改应用程序状态的控制器和其他对象共享模型实例.也许请参见使用JavaFx应用MVC .对于中等复杂的应用程序,您可能需要考虑依赖项注入框架,例如 afterburner.fx ( JavaFX专用)或Spring或Guice(不是),以便更轻松地在需要的地方注入"模型.

A cleaner approach is probably to use a MVC approach, and share a model instance with all the controllers and other objects that need to modify the application state. See, perhaps, Applying MVC With JavaFx. For moderately complex applications, you might want to consider a dependency-injection framework such as afterburner.fx (which is JavaFX-specific) or Spring or Guice (which are not) to make it easier to "inject" the model where it is needed.

这篇关于JavaFXML从所有类对Controller的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆