如何将在FXML Controller1中创建的对象传递给内部FXML控件的Controller2



我有一个JavaFX 2.0应用程序,它由两个FXML文件和两个用于它们的控制器+一个"主要"。java文件。

在启动时,FXML1被初始化,如下所示:

public void start(Stage stage) throws Exception {
    stage.setTitle("Demo Jabber JavaFX Chat");
    
    Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml"),
        ResourceBundle.getBundle("fxmlexample.fxml_example"));        
    Scene scene = new Scene(root, 226, 264);
    stage.setScene(scene);
    scene.getStylesheets().add("fxmlexample/fxmlstylesheet.css");
    stage.show();
}

然后,当单击场景1中的一个按钮时,在Controller1类中的事件处理程序中,我更改场景1根,为用户显示新的gui视图。在这个控制器中,我初始化了一些对象。例如,像这样:

public class FXMLExampleController {
   //some fields...
   private MySuperObject c;
   @FXML protected void handleSubmitButtonAction(ActionEvent event) {
    //some fields...
    c = new MySuperObject(); //here i initialize my object, i'm interested in
    try {
        c.login(username, password); // some actions with this object, which i need to make.
        Scene cc = buttonStatusText.getScene();
        Parent root = null;
        try {
            //changing a scene content...
            root = FXMLLoader.load(getClass().getResource("fxml_example2.fxml"),
            ResourceBundle.getBundle("fxmlexample.fxml_example"));
        } catch (IOException ex) {
            Logger.getLogger(FXMLExampleController.class.getName()).log(Level.SEVERE, null, ex);
        }
        cc.setRoot(root);
      }

然后,我必须在下一个场景中对该对象进行一些处理,它一定不是同一类的新实例,而是我在第一个场景中初始化的对象。

我知道如何使用";标准java";,但我对使用JavaFX+FXML执行这项任务有点困惑。

在FX 2.2中,为控制器节点引入了新的API:

// create class which is both controller and node
public class InnerFxmlControl extends HBox implements Initializable {
  @FXML public ComboBox cb;
  public InnerFxmlControl () {
     FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("fxml_example2.fxml"));
     fxmlLoader.setRoot(this);
     fxmlLoader.setController(this);
     try {
         fxmlLoader.load();            
     } catch (IOException exception) {
         throw new RuntimeException(exception);
     }
  }

带有下一个fxml(注释标签fx:root):

<fx:root type="javafx.scene.layout.HBox" xmlns:fx="http://javafx.com/fxml">
  <children>
    <ComboBox fx:id="cb" />
  </children>
</fx:root>

通过此操作,您创建了一个新控件,可以将其用作常规JavaFX控件。例如,在您的情况下:

@FXML protected void handleSubmitButtonAction(ActionEvent event) {
    // you just create new control, all fxml tricks are encapsulated
    InnerFxmlControl root = new InnerFxmlControl();
    // and you can access all its' methods and fields including matched by @FXML tag:
    root.cb.getItems().add("new item");
    Scene cc = buttonStatusText.getScene();
    cc.setRoot(root);
  }

和在fxml:中

<InnerFxmlControl />

我使用的是1.7.0_21,它现在可以这样编码:在主应用程序fxml文件中,

<VBox ...>
      <fx:include fx:id="tom" source="Tom.fxml" />
</VBox>

包含的fxml可以定义自己的fxml文件,如下所示:

<AnchorPane id="AnchorPane" fx:id="tomPan" ... xmlns:fx="http://javafx.com/fxml" fx:controller="**com.tom.fx.TomController**">

然后,在主应用程序中,控制器需要"Tom.fxml"的控制器,如下所示:

 @FXML private TomController tomController;

注意"@FXML"。也许它会自动调用控制器。

最新更新