JAVA FXCollections LoadException 类不是有效类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32502043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:18:15  来源:igfitidea点击:

JAVA FXCollections LoadException Class is not a valid type

javajavafxfxmlfxmlloader

提问by Matthis Kohli

I'm trying to implement a TableView with some data with the help of this Tutorial.

我正在尝试在本教程的帮助下使用一些数据实现 TableView 。

Im stuck on populating data from my "Person.java" to the TableView.

我坚持将数据从“Person.java”填充到 TableView。

I tracked the problem down to the part <Person firstName="Jacob" lastName="Smith" email="[email protected]" />inside the "fxmltableview.fxml" file at the very bottom.

我将问题<Person firstName="Jacob" lastName="Smith" email="[email protected]" />追溯到最底部的“fxmltableview.fxml”文件中的部分。

So it seems that for some reason my "observableArrayList" isn't allwed to contain "Person.java" objects. "String.java" is allowed. Im out of ideas for experementing with the files, as i read several times word by word through the tutorial and i don't see any difference between my files. Whenever i remove the <Person someStuff/>it works fine.

因此,由于某种原因,我的“observableArrayList”似乎并不能包含“Person.java”对象。允许使用“String.java”。我对文件的实验没有想法,因为我通过教程逐字阅读了几次,但我没有看到我的文件之间有任何区别。每当我删除<Person someStuff/>它时,它都可以正常工作。

How can i get rid of this annoying error?

我怎样才能摆脱这个烦人的错误?

Caused by: javafx.fxml.LoadException: Person is not a valid type. /D:/workspace/TableViewExampleFXML/bin/application/fxmltableview.fxml:40

引起:javafx.fxml.LoadException:Person 不是有效类型。/D:/workspace/TableViewExampleFXML/bin/application/fxmltableview.fxml:40

FXML-File:

FXML-文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import fxmltableview.*?>

<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.cell.PropertyValueFactory?>

<?import javafx.scene.layout.GridPane?>

<?import javafx.collections.FXCollections?>


<GridPane alignment="CENTER" hgap="10.0" maxHeight="-Infinity"
    maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
    prefHeight="400.0" prefWidth="600.0" vgap="10.0" xmlns="http://javafx.com/javafx/8"
    xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.FXMLTableViewController">

    <TableView fx:id="tableView" prefHeight="200.0" prefWidth="200.0"
        GridPane.columnIndex="0" GridPane.rowIndex="1">
        <columns>
            <TableColumn prefWidth="75.0" text="First Name">
                <cellValueFactory>
                    <PropertyValueFactory property="firstName" />
                </cellValueFactory>
            </TableColumn>
            <TableColumn prefWidth="75.0" text="Last Name">
                <cellValueFactory>
                    <PropertyValueFactory property="lastName" />
                </cellValueFactory>
            </TableColumn>
            <TableColumn prefWidth="75.0" text="Email Address">
                <cellValueFactory>
                    <PropertyValueFactory property="email" />
                </cellValueFactory>
            </TableColumn>
        </columns>
        <items>
            <FXCollections fx:factory="observableArrayList">
                <Person firstName="Jacob" lastName="Smith" email="[email protected]" />
            </FXCollections>
        </items>
    </TableView>
</GridPane>

The "Person.java" is copy/pasted from the tutorial thats why im so frustrated that it doesn't work for me. Anyway i will paste it here.

“Person.java”是从教程中复制/粘贴的,这就是为什么我如此沮丧以至于它对我不起作用。无论如何我会把它贴在这里。

package application;

import javafx.beans.property.SimpleStringProperty;

public class Person {
  private final SimpleStringProperty firstName = new SimpleStringProperty("");
  private final SimpleStringProperty lastName = new SimpleStringProperty("");
  private final SimpleStringProperty email = new SimpleStringProperty("");

  public Person() {
    this("", "", "");
  }

  public Person(String firstName, String lastName, String email) {
    setFirstName(firstName);
    setLastName(lastName);
    setEmail(email);
  }

  public String getFirstName() {
    return firstName.get();
  }

  public void setFirstName(String fName) {
    firstName.set(fName);
  }

  public String getLastName() {
    return lastName.get();
  }

  public void setLastName(String fName) {
    lastName.set(fName);
  }

  public String getEmail() {
    return email.get();
  }

  public void setEmail(String fName) {
    email.set(fName);
  }
}

回答by James_D

You changed the package name for your Personclass. In the tutorial, the package name is fxmltableview, where you have application. You didn't change the import accordingly. So you need

您更改了Person类的包名称。在本教程中,包名称为fxmltableview,其中包含application. 您没有相应地更改导入。所以你需要

<?import application.Person ?>

instead of

代替

<?import fxmltableview.* ?>