JAVA FXCollections LoadException 类型无效

7

我正在尝试通过这个教程的帮助,实现一个 TableView 并填充一些数据。

我卡在了从 "Person.java" 将数据填充到 TableView 上的过程中。

经过追踪,我发现问题出在了 "fxmltableview.fxml" 文件的末尾的 <Person firstName="Jacob" lastName="Smith" email="jacob.smith@example.com" /> 这一部分。

所以似乎由于某种原因我的 "observableArrayList" 不允许包含 "Person.java" 对象,但却允许 "String.java" 对象。我已经尝试了多次对文件进行实验,反复阅读教程也没有找到与我的文件之间的任何区别。只要我删除 <Person someStuff/> 部分,代码就可以正常运行。

我该如何摆脱这个烦人的错误?

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

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="jacob.smith@example.com" />
            </FXCollections>
        </items>
    </TableView>
</GridPane>

“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);
  }
}
1个回答

10

您已更改Person类的包名称。在教程中,包名称为fxmltableview,而您使用了application。您没有相应地更改导入。因此您需要:

<?import application.Person ?>

取代

<?import fxmltableview.* ?>

工作得非常完美。非常感谢您。浪费了这么多时间,却没有想到这一点。 - Matthis Kohli

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接