JavaFX如何在被叫控制器

时间:2017-08-31 01:37:03

标签: java javafx-8 fxml netbeans-8

我也是JavaFX(和Java)新手以及新手StackOverflow用户,所以如果我出错了,我会提前道歉!

我正在尝试创建一个具有主场景的应用,该场景将多个不同的场景加载为窗格。每个不同的场景都由主场景上的按钮加载(就像标签窗格一样)。

我在主屏幕上有一个标签,用于显示错误消息。我希望这些场景能够在遇到错误时更新主要场景的标签文本,但我很难知道如何做到这一点。我用谷歌搜索并尝试了几十条建议无济于事。有人能告诉我我做错了吗?非常感谢!

我创建了一个测试应用程序,显示了我想要做的事情。我使用的是JavaSE8和NetBeans8.2。

主要应用:

public class TestApp extends Application {

@Override
public void start(Stage stage) throws Exception {

    Parent root = FXMLLoader.load(getClass().getResource("FXMLMain.fxml"));
    Scene scene = new Scene(root);

    stage.setScene(scene);

    stage.show();
}

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

}

FXMLMain.fxml

<AnchorPane fx:id="anchorMain" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="testapp.FXMLMainController">
   <children>
      <AnchorPane fx:id="anchorView" prefHeight="400.0" prefWidth="400.0" />
      <Button fx:id="btnLoadView1" layoutX="476.0" layoutY="39.0" mnemonicParsing="false" onAction="#loadView1" text="Load view 1" />
      <Label fx:id="lblSystemMessage" layoutX="411.0" layoutY="145.0" text="label for system messages" />
   </children>
</AnchorPane>

FXMLMainController.java

public class FXMLMainController implements Initializable {

@FXML
private AnchorPane anchorMain;
@FXML
private AnchorPane anchorView;
@FXML
private Button btnLoadView1;
@FXML
private Label lblSystemMessage;

Pane paneView1;

@Override
public void initialize(URL url, ResourceBundle rb) {

    //Load View 1
    try {
        paneView1 = FXMLLoader.load(getClass().getResource("FXMLView1.fxml"));
    } catch (IOException ex) {
        Logger.getLogger(FXMLMainController.class.getName()).log(Level.SEVERE, null, ex);
    }
    anchorMain.getChildren().add(paneView1);
    paneView1.setVisible(false);

    //Load Views 2, 3, 4 et al.

}    

@FXML
private void loadView1(ActionEvent event) {

    paneView1.setVisible(true);

}


public void setSystemMessage (String text) {

    System.out.println("received text " + text);        
    lblSystemMessage.setText(text);
    System.out.println("lblSystemMessage.getText() is " + lblSystemMessage.getText());

}

}

FXMLView1.fxml

<AnchorPane id="AnchorPane" fx:id="anchorView1" prefHeight="400.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="testapp.FXMLView1Controller">
   <children>
      <Label layoutX="64.0" layoutY="45.0" text="This is view 1" />
      <Button fx:id="btnView1SimulateError" layoutX="64.0" layoutY="122.0" mnemonicParsing="false" onAction="#view1SimulateError" text="Simulate error" />
   </children>
</AnchorPane>

FXMLView1Controller.java

public class FXMLView1Controller implements Initializable {


@FXML
private AnchorPane anchorView1;
@FXML
private Button btnView1SimulateError;

/**
 * Initializes the controller class.
 */
@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}    

@FXML
private void view1SimulateError(ActionEvent event) {

    System.out.println("simulating error from View 1");

    FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLMain.fxml"));
    try {
        Parent root = loader.load();
    } catch (IOException ex) {
        Logger.getLogger(FXMLView1Controller.class.getName()).log(Level.SEVERE, null, ex);
    }

    FXMLMainController mainController = loader.getController();    
    mainController.setSystemMessage("FRED");        

}


}

当我运行应用程序并单击btnLoadView1然后单击btnView1SimulateError时,我从FXMLMainController中的setSystemMessage方法获得以下输出:

simulating error from View 1
received text FRED
lblSystemMessage.getText() is FRED

但主要场景中的标签文字未更新为&#34; FRED&#34;。我做错了什么?

1 个答案:

答案 0 :(得分:0)

据我所知,您想从 FXMLView1Controller 调用 FXMLMainController setSystemMessage()函数。问题是要这样做,你需要引用主控制器。所以,你需要提供一个。

这是可能的解决方案之一。您从主控制器的initialize功能加载FXMLView1.fxml。因此,您可以获得在负载中分配的控制器。这是一个例子:

    FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLView1.fxml"));
    try {
        paneView1 = loader.load();
        FXMLView1Controller view1Controller = loader.getController();
    }
    catch (IOException e) {
         ...
    }

现在你有了view1Controller。你可以做任何你想做的事。我建议您添加 mainController 字段并执行

 view1Controller.mainController = this;

以后需要的时候,

 mainController.setSystemMessage(message);