带有控制器的多个 FXML,共享对象 [英] Multiple FXML with Controllers, share object

查看:33
本文介绍了带有控制器的多个 FXML,共享对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家晚上好,

我已经找到了很多关于这个主题的帖子,但我仍然无法将对象从 Controller1 传递到 Controller2.是否有完整的教程或一些示例项目可以做到这一点?

I have found a bunch of posts already on this topic but I still can not manage to pass an object from Controller1 to Controller2. Is there somewhere a full tutorial or some example project that does this?

我已经走到这一步,直到我卡住了:

I've gotten this far until I got stuck:

乡村班

public class Country {
private SimpleStringProperty country = new SimpleStringProperty("");

//Constructor
public Country() {
}

//GETTERS
public String getCountry() {
    return country.get();
}

//SETTERS
public void setCountry(String value) {
    country.set(value);
}

@Override
public String toString() {
    return getCountry();
}
}

当程序启动时,主 FXML 被加载 (Sample.fxml).这包含一个边框窗格,顶部面板中有一个菜单栏,中间有一个内容窗格.在初始化时,我创建了一个新的 Country 对象并将其存储在一个全局变量中.我有一种方法可以在单击菜单项时将另一个 FXML 加载到内容窗格中:

When the program starts, the main FXML gets loaded (Sample.fxml). This contains a border pane with a menu bar in the top panel and a content pane in the center. On initialize I create a new Country object and store it in a global variable. I have a method that loads another FXML into the content pane when a menu item is clicked:

SampleController.java

public class SampleController implements Initializable {

@FXML
private Pane pContent;

private Country c;

@FXML
private void handleButtonAction(ActionEvent event) throws IOException {
    System.out.println(c); //this prints Belgium, which is correct

    URL url = getClass().getResource("Sub1.fxml");

    FXMLLoader fxmlloader = new FXMLLoader();
    fxmlloader.setLocation(url);
    fxmlloader.setBuilderFactory(new JavaFXBuilderFactory());

    pContent.getChildren().clear();
    pContent.getChildren().add((Node) fxmlloader.load(url.openStream()));
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    c = new Country();
    c.setCountry("Belgium");
}

public Country getCountryFromSampleController(){
    return c;
}
}

现在我希望在加载 Sub1.fxml 时捕获 Country 对象,这意味着我需要在 initialize() 上获取 country 对象:

Now I wish to capture the Country object when the Sub1.fxml gets loaded, which means I need to fetch the country object on initialize():

Sub1Controller.java

public class Sub1Controller implements Initializable {

/**
 * Initializes the controller class.
 */
@Override
public void initialize(URL url, ResourceBundle rb) {
    SampleController sp = new SampleController(); //I don't know how to fetch the original SampleController object
    System.out.println(sp.getCountryFromSampleController()); 
    //this prints null, which is ofcourse logical because I make a new SampleController object.         
}    
}

我的问题是,如何获取原始"SampleController 对象,以便我可以使用 getCountryFromRoot() 方法获取值为 Belgium 的 Country 对象?我已经在这个问题上搜索了好几个小时,并阅读了 StackOverflow 上关于此的所有帖子,但似乎我没有找到丢失的链接......感谢任何帮助(最好使用此代码)!

The question that I have, how can I get the 'original' SampleController object so I can use the getCountryFromRoot() method to fetch the Country object with value Belgium? I've been searching on this issue for hours and hours and have read every post on StackOverflow about this, but it seems I do not find the missing link... any help (preferably with this code) is appreciated!

抱歉,帖子太长了,我尽量做到完整,否则我永远无法理解...

Sorry for the long post, I tried to be as complete as possible else I'll never understand...

推荐答案

FXML 是 MVC 模式.FXML 文件是一个视图,Controller 很明显,错过了什么?模型 -- 一个存储与当前视图相关的所有数据的地方,因此,您可以使用它在控制器之间共享国家/地区数据.

FXML is a simple form of MVC pattern. FXML file is a view, Controller is obvious, what's missed? The model -- a place where you store all data relative to your current view and, thus, which you can use to share Country data between controllers.

1. 引入模型的一种可能方法是上下文".让我们考虑一个案例,那么整个项目只有一个模型,因此您可以拥有 Singleton

1. One of the possible approach to introduce model is "context". Let's consider a case, then you have only one model for the whole project so you can have a global context in a form of Singleton

public class Context {
    private final static Context instance = new Context();

    public static Context getInstance() {
        return instance;
    }

    private Country country = new Country();

    public Country currentCountry() {
        return country;
    }
}

您的 SampleController 将有以下更改:

Your SampleController will have next changes:

@Override
public void initialize(URL url, ResourceBundle rb) {
    Context.getInstance().currentCountry().setCountry("Belgium");
}

SubController1 可以用同样的方式访问它:

And SubController1 can access it the same way:

@Override
public void initialize(URL url, ResourceBundle rb) {
    System.out.println(Context.getInstance().currentCountry().getCountry());
}

<小时>

2. 另一种方法是将上下文传递给 SubController1 然后加载它的 xml.如果您不想拥有应用程序全局模型,它会更好地工作.所以创建类似的 Context 类但没有实例字段,并且:


2. Another way is to pass context to SubController1 then you load it's xml. It will work better if you don't want to have application global model. So create similar Context class but without instance fields, and:

public class Sub1Controller implements Initializable {
    private Context context;
    public void setContext(Context context) {
        this.context = context;
        // initialize country dependent data here rather then in initialize()
    }
}

SampleController中设置上下文:

Context currentContext = new Context();

@Override
public void initialize(URL url, ResourceBundle rb) {
    currentContext.currentCountry().setCountry("Belgium");
}

@FXML
private void handleButtonAction(ActionEvent event) throws IOException {
    URL url = getClass().getResource("Sub1.fxml");

    FXMLLoader fxmlloader = new FXMLLoader();
    fxmlloader.setLocation(url);
    fxmlloader.setBuilderFactory(new JavaFXBuilderFactory());

    pContent.getChildren().clear();
    pContent.getChildren().add((Node) fxmlloader.load(url.openStream()));
            // here we go
    ((Sub1Controller)fxmlloader.getController()).setContext(currentContext);
}

这篇关于带有控制器的多个 FXML,共享对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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