将使用Javafx创建的节点或场景添加到fxml

将使用Javafx创建的节点或场景添加到fxml,javafx,scenebuilder,Javafx,Scenebuilder,我试图添加一个通过编码在拆分窗格右侧创建的TreeTableView。SplitPane具有修复Id:splitId,其右侧具有Id SplitdRight。我已经创建了splitpane,并通过SceneBuilder将其添加到anchorepane中。然后我用菜单栏将边框窗格装箱。然后我将splitpane添加到BorderPane的中心。现在我想将树表添加到splitpane的右侧。错误是javafx.scene.layout.ancorpane无法转换为javafx.scene.cont

我试图添加一个通过编码在拆分窗格右侧创建的TreeTableView。SplitPane具有修复Id:splitId,其右侧具有Id SplitdRight。我已经创建了splitpane,并通过SceneBuilder将其添加到anchorepane中。然后我用菜单栏将边框窗格装箱。然后我将splitpane添加到BorderPane的中心。现在我想将树表添加到splitpane的右侧。错误是
javafx.scene.layout.ancorpane无法转换为javafx.scene.control.SplitPane
,但除了此错误之外,我不确定是否在正确的位置插入

public class Main extends Application {
private DataConstructor dc = new DataConstructor();
private BorderPane rootLayout;
private Stage primaryStage;


TreeItem<String> root = new TreeItem<>("Functions");

public static void main(String[] args) {
    Application.launch(Main.class, args);
}

@Override
public void start(Stage primaryStage) throws Exception {
    this.primaryStage = primaryStage;
    primaryStage.setTitle("IT-Saturation");

    initRootLayout();
    showOverViw();
    makeTreeTable();
}

private void makeTreeTable() {
    Scene scene = new Scene(new Pane(), 1200, 1800);
    Pane sceneRoot = (Pane) scene.getRoot();
    root.setExpanded(true);
    //........make treetable.....//

    treeTableView.setPrefWidth(1200);
    treeTableView.setShowRoot(false);
    treeTableView.setTableMenuButtonVisible(true);
     sceneRoot.getChildren().add(treeTableView);

    SplitPane sp = null;
    try {
        sp = FXMLLoader.load(getClass().getResource("/view/OverView.fxml"));


    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    StackPane container = new StackPane();
    container.getChildren().add(treeTableView);
    sp.getItems().add(container);
    sp.setDividerPositions(0.3f, 0.6f, 0.9f); // you can tweak it any how

    // primaryStage.setScene(scene);
    // primaryStage.show();

}


/**
 * Initializes the root layout.
 */
public void initRootLayout() {
    try {
        // Load root layout from fxml file.
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("/view/RootLayout.fxml"));
        rootLayout = (BorderPane) loader.load();

        // Show the scene containing the root layout.
        Scene scene = new Scene(rootLayout);
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

/**
 * Shows the overview inside the root layout.
 */
public void showOverViw() {
    try {
        // Load overview.
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("/view/OverView.fxml"));
        AnchorPane overView = (AnchorPane) loader.load();

        // Set overview into the center of root layout.
        rootLayout.setCenter(overView);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

/**
 * Returns the main stage.
 * 
 * @return
 */
public Stage getPrimaryStage() {
    return primaryStage;
}
public类主扩展应用程序{
私有DataConstructor dc=新DataConstructor();
私有边界布局;
私人阶段初级阶段;
TreeItem root=新的TreeItem(“函数”);
公共静态void main(字符串[]args){
Application.launch(Main.class,args);
}
@凌驾
public void start(Stage primaryStage)引发异常{
this.primaryStage=primaryStage;
初级阶段。设置标题(“IT饱和度”);
initRootLayout();
showOverViw();
makeTreeTable();
}
私有void makeTreeTable(){
场景=新场景(新窗格(),1200,1800);
窗格sceneRoot=(窗格)scene.getRoot();
root.setExpanded(true);
//……使树变得可编辑//
treeTableView.setPrefWidth(1200);
treeTableView.setShowRoot(false);
treeTableView.setTableMenuButtonVisible(true);
sceneRoot.getChildren().add(treeTableView);
SplitPane sp=null;
试一试{
sp=fxmloader.load(getClass().getResource(“/view/OverView.fxml”);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
StackPane容器=新的StackPane();
container.getChildren().add(treeTableView);
sp.getItems().add(容器);
sp.setDividerPositions(0.3f、0.6f、0.9f);//您可以任意调整它
//初级阶段。场景(场景);
//primaryStage.show();
}
/**
*初始化根布局。
*/
public void initRootLayout(){
试一试{
//从fxml文件加载根布局。
FXMLLoader=新的FXMLLoader();
setLocation(Main.class.getResource(“/view/RootLayout.fxml”);
rootLayout=(BorderPane)loader.load();
//显示包含根布局的场景。
场景=新场景(根布局);
初级阶段。场景(场景);
primaryStage.show();
}捕获(IOE异常){
e、 printStackTrace();
}
}
/**
*显示根布局内部的概述。
*/
公共空间展示(){
试一试{
//加载概述。
FXMLLoader=新的FXMLLoader();
setLocation(Main.class.getResource(“/view/OverView.fxml”);
AnchorPane overView=(AnchorPane)loader.load();
//将概览设置为根布局的中心。
rootLayout.setCenter(概述);
}捕获(IOE异常){
e、 printStackTrace();
}
}
/**
*返回主阶段。
* 
*@返回
*/
公共阶段getPrimaryStage(){
返回初级阶段;
}
这是rootLayout.fxml(边框窗格)


错误提示您正试图将OverView.fxml的上根节点AnchorPane强制转换为SplitPane

换线

SplitPane = null;

这应该可以解决您的错误。

请使用,这样您就可以访问FXML中定义的UI元素。
AnchorPane =null;