我尝试添加一个TreeTableView,我通过编码创建了我的splitpane的右侧。 SplitPane具有修复Id:splitId,其右侧具有Id splitidRight。我创建了splitpane并通过scenebuiler将它添加到anchorepane中。然后我用菜单栏包装了BorderPane。然后我将splitpane添加到BorderPane的中心。现在我想在分裂板的右侧添加treetable。错误是javafx.scene.layout.AnchorPane cannot be cast to 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;
}
这是rootLayout.fxml(BorderPane)
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<top>
<MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
</BorderPane>
&#13;
这是带有splitpane的OverView.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="700.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<SplitPane fx:id="splitId" dividerPositions="0.14691151919866444" layoutX="58.0" layoutY="42.0" prefHeight="700.0" prefWidth="1200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" SplitPane.resizableWithParent="false">
<children>
<Label layoutX="23.0" layoutY="173.0" text="Function count" />
<TextField editable="false" layoutX="1.0" layoutY="225.0" promptText="FuncCount" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="1.0" />
<Label layoutX="20.0" layoutY="315.0" text="Organization count" />
<TextField editable="false" layoutX="1.0" layoutY="366.0" promptText="OrgCount" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="1.0" />
<ToggleButton layoutX="30.0" layoutY="487.0" mnemonicParsing="false" text="Draw/Reset" />
</children>
</AnchorPane>
<AnchorPane fx:id="splitIdRight" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" />
</items>
</SplitPane>
</children>
</AnchorPane>
&#13;
答案 0 :(得分:0)
正如错误所示,您正在尝试将AnchorPane(OverView.fxml的顶级根节点)转换为SplitPane。
更改行
SplitPane = null;
到
AnchorPane =null;
应解决您的错误。