如何在Controller类中从Main类获取Stackpane?

时间:2015-05-08 15:28:16

标签: javafx controller parent fxml fxmlloader

为了熟悉场景构建器,我在场景构建器的堆栈窗格中添加了一个线图和两个numberaxis作为节点。 父节点将加载到mainApp.java中:

public class CsvCommander extends Application {    
    @Override
    public void start(Stage stage) throws Exception {        
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));           
        Scene scene = new Scene(root);        
        stage.setScene(scene);
        stage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }   
}

现在,为了进一步操作,我想在FXMLDocument.fxml中获取父窗口的堆栈窗格,但我不知道如何...

e.g。

StackPane容器= FXMLLoader.load(getClass()。getResource(“FXMLDocument.fxml”)等。

如何在Controller传递中获取我的根节点或堆栈窗口?

1 个答案:

答案 0 :(得分:1)

fx:id放在FXML文件的根元素上,然后以与其他元素相同的方式将其注入控制器:

FXML文件:

<!-- imports etc -->
<StackPane xmlns="..." fx:controller="com.example.MyControllerClass" fx:id="container">
    <!-- nodes etc -->
</StackPane>

控制器:

public class MyControllerClass {

    @FXML // will be initialized by FXMLLoader
    private StackPane container ;

    public void initialize() {
        // do something with container....
    }
}
相关问题