borderpane中有多个fxmls,无法获取父级或共享控制器

时间:2017-02-24 01:22:44

标签: java javafx fxml

ToolBar.java

package util;

import javafx.scene.Parent;
import javafx.scene.layout.BorderPane;

public class ToolBar {    
    private static BorderPane borderPane;

    public void setBorderPane(BorderPane borderPane) {
        this.borderPane = borderPane;
    }    
    public static void initialize(BorderPane borderPane) {
        ToolBar toolBar = new ToolBar();
        toolBar.setBorderPane(borderPane);
    }
    public static void show(Parent parent) {
        borderPane.setLeft(parent);
    }
    public static void hide() {
        borderPane.setLeft(null);
    }
}

HomeController.java

package main;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import util.MainContent;
import util.ToolBar;

import java.net.URL;
import java.util.ResourceBundle;

public class HomeController implements Initializable {    
    @FXML
    private Button editorButton;
    @FXML
    private BorderPane borderPane;

    private MainContent mainContent;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        mainContent = new MainContent(borderPane);
    }    
    @FXML
    private void goToEditor() throws Exception {
        mainContent.show(getClass().getResource("editor.fxml"));
                        ToolBar.show(FXMLLoader.load(getClass().getResource("toolBar.fxml")));
    }
}

我正在尝试使用工具栏按钮来访问边框。例如将新内容加载到父边框窗格,但将setCenter加载到另一个fxml文件。

我尝试使用带有

工具栏的元素
borderpane = (BorderPane) toolBar.getParent();
borderpane.setCenter("someFXML.fxml");

但是出现了NullPointerException错误,有什么建议吗?感谢。

0 个答案:

没有答案