使用 Scene builder 在 JavaFX 中制作一个简单的树视图

Making a simple treeview in JavaFX with Scene builder

我正在尝试创建一个简单的树视图并填充它,但是我得到了一个 InvocationTargetException。

这是控制器的代码:

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);
    }
}

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$launchApplication2(LauncherImpl.java:182)
    at com.sun.javafx.application.LauncherImpl$$Lambda/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$launchApplication19(LauncherImpl.java:863)
    at com.sun.javafx.application.LauncherImpl$$Lambda/746391480.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait2(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl$$Lambda/355629945.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null0(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda/64985315.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater1(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda/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$null5(WinApplication.java:101)
    at com.sun.glass.ui.win.WinApplication$$Lambda/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.access0(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

它是否必须对 fxmlloader 做些什么?如果我不包括这个,我可以 运行 程序,但我只是得到一个空白屏幕,因为 FXML 没有加载。这部分需要看起来像什么?

我是 JavaFX 的新手,所以这可能是一个菜鸟问题:)

您的控制器来自 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">

更改类型,使其相互匹配,错误应该消失。