javafx - 使用 Scene builder 在 JavaFX 中制作一个简单的 TreeView

标签 javafx treeview scenebuilder

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

这是 Controller 的代码:

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$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

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

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

最佳答案

您的 Controller 扩展自 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">

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

关于javafx - 使用 Scene builder 在 JavaFX 中制作一个简单的 TreeView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29341909/

相关文章:

java - 如何创建弹出消息来提醒用户字段不完整?

java - javaFX中的JFXSlider NullPointerException

java - 这可以被认为是人工智能吗?

java - 不匹配的 Javafx fx :id and variable name in controller class

java - JavaFx 中的生产者-消费者应用程序(从任务到 "update"两个文本字段获取正确值的问题)

java - 使用 html 布局创建 JavaFX 弹出窗口

JavaFX Button 释放事件

java - 实现基于节点的图形 GUI 的最佳/最简单方法?

C# WinForms 如何在 TreeView 中停止丁声

WPF:TreeView 中的自定义 +-