我想要在带有FXMLoader
的场景中找到一个FXMLoader
节点,这要感谢Node#lookup()
,但是我得到了以下例外:
java.lang.ClassCastException: com.sun.javafx.scene.control.skin.SplitPaneSkin$Content cannot be cast to javafx.scene.layout.VBox
代码:
public class Main extends Application {
public static void main(String[] args) {
Application.launch(Main.class, (java.lang.String[]) null);
}
@Override
public void start(Stage stage) throws Exception {
AnchorPane page = (AnchorPane) FXMLLoader.load(Main.class.getResource("test.fxml"));
Scene scene = new Scene(page);
stage.setScene(scene);
stage.show();
VBox myvbox = (VBox) page.lookup("#myvbox");
myvbox.getChildren().add(new Button("Hello world !!!"));
}
}
fxml文件:
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml" >
<children>
<SplitPane dividerPositions="0.5" focusTraversable="true" prefHeight="400.0" prefWidth="600.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" />
<VBox fx:id="myvbox" prefHeight="398.0" prefWidth="421.0" />
</items>
</SplitPane>
</children>
</AnchorPane>
我想知道:
SplitPaneSkin$Content
而不是VBox
?VBox
?提前感谢
发布于 2012-09-07 21:16:57
SplitPaneSkin$Content
)。由于未知的原因,FXMLLoader为它们分配了与根子用户相同的id。您可以通过下一个实用程序方法获得所需的VBox:
公共T查找(节点父、字符串id、类clazz) { for (节点节点: parent.lookupAll( id )) { if (node.getClass().isAssignableFrom(clazz)) {返回(T)节点;}抛出新的IllegalArgumentException(“父”+父+“不包含带有id”+id的节点);
然后用下一个方法:
VBox myvbox =查找(页面,"#myvbox",VBox.class);myvbox.getChildren().add(新按钮(“Hello!!”));https://stackoverflow.com/questions/12324799
复制相似问题