简体   繁体   English

从Java FX 2.2上的外部控制器加载FXML

[英]Loading FXML from external controller on Java FX 2.2

I'm currently recoding a small application from Swing to JavaFX, since it seemed like the easiest way to deploy what I currently have for web. 我目前正在将一个小型应用程序从Swing编码为JavaFX,因为这似乎是将我当前拥有的Web部署最简单的方法。

I can't seem to do something very simple, and I'm getting lost on the documentation and other posts: 我似乎无法做一些非常简单的事情,并且迷失在文档和其他文章中:

Anyway, I have my main controller that calls the associated FXML file: 无论如何,我有我的主控制器来调用关联的FXML文件:

public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));

    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.show();
}

Now, from Login.fxml, I have a button and I want that button to open another FXML file. 现在,从Login.fxml中,我有一个按钮,我希望该按钮打开另一个FXML文件。 I can get the button to be sucesfully load the event but I tried many things and I can't get it to work. 我可以获取成功加载事件的按钮,但是尝试了很多事情,但无法正常工作。 I'm trying something like this: 我正在尝试这样的事情:

private void handleButtonAction(ActionEvent event){

    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("GeneradorBases.fxml"));

   // fxmlLoader.setRoot(this); 
  //  fxmlLoader.setController(this);

    try {
        fxmlLoader.load(); 

    }
    catch (IOException e){
       throw new RuntimeException(e); 
    }
}

I tried to follow an example I saw here on Stackoverflow. 我尝试遵循在Stackoverflow上看到的示例。 Basically the .setRoot and .setController crash the application. 基本上.setRoot和.setController使应用程序崩溃。 Even the .load() does that too. 甚至.load()也这样做。

Any advice on how I can make this work? 关于如何进行这项工作有什么建议吗?

Given exception you listed you need to set all fields marked with @FXML and class packet.OtherControllerController public . 给定例外,您需要列出所有标记有@FXML和class packet.OtherControllerController public的字段。

Update: you another problem is that you don't assign result of FXML load to anything. 更新:另一个问题是您没有将FXML加载的结果分配给任何东西。

public class FirstController implements Initializable {

  @FXML
  public void handleButtonAction(ActionEvent event) throws Exception{
    Node node = (Node) event.getSource();
    Stage stage = (Stage) node.getScene().getWindow();
    Scene scene = stage.getScene();

    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Second.fxml"));
    Parent root = (Parent) fxmlLoader.load();          

    scene.setRoot(root);
  }
}

See tutorial for extended example: http://docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm 请参阅教程以获取扩展示例: http : //docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm

Your solution does nothing with the node loaded by fxmlLoader.load() . 您的解决方案对fxmlLoader.load()加载的节点不执行任何操作。 In order to show the loaded FXML file, you need to pass the returned value somehow to your main controller (or another part of the code that should manage the stage or scene) and set it as the new root (or eg child of a pane in which it should be shown). 为了显示加载的FXML文件,您需要以某种方式将返回的值传递到主控制器(或应该管理舞台或场景的代码的另一部分),并将其设置为新的根(例如,窗格的子级)应该在其中显示)。

If presented solutions don't will be helpful, you can try following code (for me it is work): 如果提出的解决方案没有帮助,您可以尝试以下代码(对我来说,这是可行的):

final FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/page.fxml"));
      Parent root = (Parent) fxmlLoader.load();

      Scene nscene = new Scene(root);
      Stage tStatge = new Stage();
      tStatge.setScene(nscene);
      tStatge.show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM