使用场景生成器在JavaFX中创建简单的树视图
我试图创建一个简单的树视图并填充它,但是我得到了一个InvocationTargetException 这是控制器的代码:使用场景生成器在JavaFX中创建简单的树视图,javafx,treeview,scenebuilder,Javafx,Treeview,Scenebuilder,我试图创建一个简单的树视图并填充它,但是我得到了一个InvocationTargetException 这是控制器的代码: public class MainPanel extends BorderPane { @FXML TreeView selectionTreeView; public MainPanel(){ FXMLLoader loader = new FXMLLoader(getClass().getResource("MainPanel.
public class MainPanel extends BorderPane {
@FXML
TreeView selectionTreeView;
public MainPanel(){
FXMLLoader loader = new FXMLLoader(getClass().getResource("MainPanel.fxml"));
loader.setRoot(this);
loader.setController(this);
try {
loader.load();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
//create root
TreeItem<String> root = new TreeItem<>("Root");
//root.setExpanded(true);
//create child
TreeItem<String> itemChild = new TreeItem<>("Child");
itemChild.setExpanded(false);
//root is the parent of itemChild
root.getChildren().add(itemChild);
selectionTreeView.setRoot(root);
}
}
public类主面板扩展边框窗格{
@FXML
TreeView选择TreeView;
公共主面板(){
FXMLLoader=新的FXMLLoader(getClass().getResource(“MainPanel.fxml”);
setRoot(this);
loader.setController(此);
试一试{
loader.load();
}捕获(IOEX异常){
抛出新的运行时异常(ex);
}
//创建根
TreeItem root=新的TreeItem(“根”);
//root.setExpanded(true);
//创建子对象
TreeItemChild=新的TreeItem(“孩子”);
itemChild.setExpanded(false);
//root是itemChild的父级
root.getChildren().add(itemChild);
选择treeview.setRoot(root);
}
}
fxml代码:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<fx:root maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" type="GridPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="291.0" minWidth="10.0" prefWidth="176.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="456.0" minWidth="10.0" prefWidth="424.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<TreeView fx:id="selectionTreeView" prefHeight="200.0" prefWidth="200.0" />
</children>
</fx:root>
这就是我得到的错误:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182)
at com.sun.javafx.application.LauncherImpl$$Lambda$50/1642360923.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: javafx.fxml.LoadException: Root is not an instance of javafx.scene.layout.GridPane.
file:/D:/Stijn/Documenten/NetBeansProjects/groep11Java/climateChartProject/dist/run75612214/ClimateChartProject.jar!/gui/MainPanel.fxml:9
at gui.MainPanel.<init>(MainPanel.java:34)
at StartUp.start(StartUp.java:22)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/746391480.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/355629945.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/64985315.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1915503092.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/1963387170.run(Unknown Source)
... 1 more
Caused by: javafx.fxml.LoadException: Root is not an instance of javafx.scene.layout.GridPane.
file:/D:/Stijn/Documenten/NetBeansProjects/groep11Java/climateChartProject/dist/run75612214/ClimateChartProject.jar!/gui/MainPanel.fxml:9
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:104)
at javafx.fxml.FXMLLoader$RootElement.constructValue(FXMLLoader.java:1326)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:742)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2711)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2531)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2413)
at gui.MainPanel.<init>(MainPanel.java:32)
... 15 more
Exception running application StartUp
java.lang.reflect.InvocationTargetException
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:497)
位于com.sun.javafx.application.LaunchImpl.launchApplicationWithArgs(LaunchImpl.java:389)
位于com.sun.javafx.application.LaunchImpl.launchApplication(LaunchImpl.java:328)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:497)
位于sun.launcher.launchelper$FXHelper.main(launchelper.java:767)
原因:java.lang.RuntimeException:应用程序启动方法中的异常
位于com.sun.javafx.application.LaunchImpl.launchApplication1(LaunchImpl.java:917)
在com.sun.javafx.application.launchempl.lambda$launchApplication$152(launchempl.java:182)
位于com.sun.javafx.application.launchempl$$Lambda$50/1642360923.run(未知源)
运行(Thread.java:745)
原因:java.lang.RuntimeException:javafx.fxml.LoadException:Root不是javafx.scene.layout.GridPane的实例。
文件:/D:/Stijn/Documenten/NetBeansProjects/groep11Java/climateChartProject/dist/run75612214/climateChartProject.jar/gui/MainPanel.fxml:9
在gui.MainPanel.(MainPanel.java:34)
StartUp.start(StartUp.java:22)
在com.sun.javafx.application.launchempl.lambda$launchApplication1$159(launchempl.java:863)
位于com.sun.javafx.application.launchempl$$Lambda$53/746391480.run(未知源)
位于com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
位于com.sun.javafx.application.PlatformImpl$$Lambda$45/355629945.run(未知源)
位于com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
位于com.sun.javafx.application.PlatformImpl$$Lambda$48/64985315.run(未知源)
位于java.security.AccessController.doPrivileged(本机方法)
位于com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
位于com.sun.javafx.application.PlatformImpl$$Lambda$47/1915503092.run(未知来源)
位于com.sun.glass.ui.invokelateDispatcher$Future.run(invokelateDispatcher.java:95)
在com.sun.glass.ui.win.WinApplication.\u runLoop(本机方法)
位于com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
在com.sun.glass.ui.win.WinApplication$$Lambda$36/1963387170.run(未知来源)
... 还有一个
原因:javafx.fxml.LoadException:Root不是javafx.scene.layout.GridPane的实例。
文件:/D:/Stijn/Documenten/NetBeansProjects/groep11Java/climateChartProject/dist/run75612214/climateChartProject.jar/gui/MainPanel.fxml:9
位于javafx.fxml.fxmloader.constructLoadException(fxmloader.java:2601)
在javafx.fxml.fxmloader.access$100(fxmloader.java:104)
在javafx.fxml.fxmloader$RootElement.constructValue(fxmloader.java:1326)
在javafx.fxml.fxmloader$ValueElement.processStartElement(fxmloader.java:742)
位于javafx.fxml.fxmloader.processStartElement(fxmloader.java:2711)
在javafx.fxml.fxmloader.loadImpl(fxmloader.java:2531)
在javafx.fxml.fxmloader.loadImpl(fxmloader.java:2445)
在javafx.fxml.fxmloader.load(fxmloader.java:2413)
在gui.MainPanel.(MainPanel.java:32)
... 还有15个
运行应用程序启动时出现异常
它可能与fxmlloader有关吗?如果我不包括这个,我可以运行这个程序,但我只是得到一个空白屏幕,因为FXML没有加载。这部分需要看起来怎么样
我是JavaFX新手,所以这可能是一个noob问题:)您的控制器从
BorderPane
public class MainPanel extends BorderPane
但是您的fxml正在将类型设置为GridPane
:
<fx:root maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"
type="GridPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
更改类型,使其相互匹配,错误就会消失。有时简单的事情很难检测到=)