gpt4 book ai didi

JavaFX PropertyValueFactory 无法检索属性

转载 作者:行者123 更新时间:2023-11-29 04:09:31 26 4
gpt4 key购买 nike

总结:

我正在尝试通过按下“重新加载按钮”将数据从 ArrayList 添加/重新加载到 TableView(目前我只是用 TableView 的一列对其进行测试)。因为 TableView 需要一个 ObservableList,所以我创建了一个 ObservableList getter 方法,其中 ObservableList 是从现有的 ArrayList 创建的(我通过打印它们的值来测试两个列表,没有问题)。每次我按下重新加载按钮时,只创建了行数并且我得到一个错误,由这行代码引起:

columnProject.setCellValueFactory(new PropertyValueFactory<Bill, String>("project"));

我在过去的 3 个小时里查找了这个问题,并尝试了我在 Stack Overflow 上找到的所有内容,但都没有成功。

信息:

  • Java:openjdk 11.0.3 和 Javafx-11
  • 操作系统:带内核 5.0.7-1 的 Manjaro Linux
  • IDE:IntelliJ Idea Ultimate 2019
  • 我将 FXML 与 SceneBuilder 一起用于所有 JavaFX 对象

错误:

Apr 26, 2019 10:23:16 PM javafx.scene.control.cell.PropertyValueFactory getCellDataReflectively
WARNING: Can not retrieve property 'project' in PropertyValueFactory: javafx.scene.control.cell.PropertyValueFactory@688d477d with provided class type: class net.strobl.main.Bill
java.lang.RuntimeException: java.lang.ClassCastException: class java.lang.String cannot be cast to class javafx.beans.property.ReadOnlyProperty (java.lang.String is in module java.base of loader 'bootstrap'; javafx.beans.property.ReadOnlyProperty is in module javafx.base of loader 'app')
at javafx.base/com.sun.javafx.property.PropertyReference.getProperty(PropertyReference.java:199)
at javafx.controls/javafx.scene.control.cell.PropertyValueFactory.getCellDataReflectively(PropertyValueFactory.java:182)
at javafx.controls/javafx.scene.control.cell.PropertyValueFactory.call(PropertyValueFactory.java:154)
at javafx.controls/javafx.scene.control.cell.PropertyValueFactory.call(PropertyValueFactory.java:133)
at javafx.controls/javafx.scene.control.TableColumn.getCellObservableValue(TableColumn.java:593)
at javafx.controls/javafx.scene.control.TableColumn.getCellObservableValue(TableColumn.java:578)
at javafx.controls/javafx.scene.control.TableCell.updateItem(TableCell.java:646)
at javafx.controls/javafx.scene.control.TableCell.indexChanged(TableCell.java:469)
at javafx.controls/javafx.scene.control.IndexedCell.updateIndex(IndexedCell.java:120)
at javafx.controls/javafx.scene.control.skin.TableRowSkinBase.updateCells(TableRowSkinBase.java:539)
at javafx.controls/javafx.scene.control.skin.TableRowSkinBase.<init>(TableRowSkinBase.java:159)
at javafx.controls/javafx.scene.control.skin.TableRowSkin.<init>(TableRowSkin.java:89)
at javafx.controls/javafx.scene.control.TableRow.createDefaultSkin(TableRow.java:213)
at javafx.controls/javafx.scene.control.Control.doProcessCSS(Control.java:897)
at javafx.controls/javafx.scene.control.Control.access$000(Control.java:83)
at javafx.controls/javafx.scene.control.Control$1.doProcessCSS(Control.java:89)
at javafx.controls/com.sun.javafx.scene.control.ControlHelper.processCSSImpl(ControlHelper.java:67)
at javafx.graphics/com.sun.javafx.scene.NodeHelper.processCSS(NodeHelper.java:145)
at javafx.graphics/javafx.scene.Node.processCSS(Node.java:9529)
at javafx.graphics/javafx.scene.Node.applyCss(Node.java:9616)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.setCellIndex(VirtualFlow.java:1715)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1692)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.getCellLength(VirtualFlow.java:1801)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.computeViewportOffset(VirtualFlow.java:2639)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1245)
at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1204)
at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1211)
at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1211)
at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1211)
at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1211)
at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1211)
at javafx.graphics/javafx.scene.Scene.doLayoutPass(Scene.java:576)
at javafx.graphics/javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2482)
at javafx.graphics/com.sun.javafx.tk.Toolkit.lambda$runPulse$2(Toolkit.java:412)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:411)
at javafx.graphics/com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:438)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:519)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:499)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:492)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$11(QuantumToolkit.java:320)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.ClassCastException: class java.lang.String cannot be cast to class javafx.beans.property.ReadOnlyProperty (java.lang.String is in module java.base of loader 'bootstrap'; javafx.beans.property.ReadOnlyProperty is in module javafx.base of loader 'app')
at javafx.base/com.sun.javafx.property.PropertyReference.getProperty(PropertyReference.java:197)
... 44 more

代码:

注意:为了更好的可读性,我在这里删除了一些与这个问题无关的方法

账单类:

package net.strobl.main;

public class Bill {

private String project;
private boolean intake;
private Double amount;
private boolean digital;
private String date1;
private String date2;
private String date3;
private String shop;
private String reason;
private String person;
private boolean paid;

public Bill(String project, boolean intake, Double amount, boolean digital, String date1, String date2, String date3, String shop, String reason, String person, boolean paid) {
this.project = project;
this.intake = intake;
this.amount = amount;
this.digital = digital;
this.date1 = date1;
this.date2 = date2;
this.date3 = date3;
this.shop = shop;
this.reason = reason;
this.person = person;
this.paid = paid;
}

//region Getters and Setters
public String getProject() {
return project;
}

public String projectProperty() {
return project;
}

public void setProject(String project) {
this.project = project;
}

public boolean isIntake() {
return intake;
}

public void setIntake(boolean intake) {
this.intake = intake;
}

public Double getAmount() {
return amount;
}

public void setAmount(Double amount) {
this.amount = amount;
}

public boolean isDigital() {
return digital;
}

public void setDigital(boolean digital) {
this.digital = digital;
}

public String getDate1() {
return date1;
}

public void setDate1(String date1) {
this.date1 = date1;
}

public String getDate2() {
return date2;
}

public void setDate2(String date2) {
this.date2 = date2;
}

public String getDate3() {
return date3;
}

public void setDate3(String date3) {
this.date3 = date3;
}

public String getShop() {
return shop;
}

public void setShop(String shop) {
this.shop = shop;
}

public String getReason() {
return reason;
}

public void setReason(String reason) {
this.reason = reason;
}

public String getPerson() {
return person;
}

public void setPerson(String person) {
this.person = person;
}

public boolean isPaid() {
return paid;
}

public void setPaid(boolean paid) {
this.paid = paid;
}
//endregion
}

Controller 类:

package net.strobl.main;


import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;

import javafx.scene.control.*;

import javafx.scene.control.cell.PropertyValueFactory;
import net.strobl.processing.DataManager;

import java.io.IOException;


public class Controller{

private App app;
private DataManager dataManager;


//region @FXML Objects new Bill
@FXML
Button submitButton;

@FXML
TextField inputProject;
@FXML
TextField inputAmount;
@FXML
TextField inputShop;
@FXML
TextField inputReason;
@FXML
TextField inputPerson;
@FXML
TextField inputDate1;
@FXML
TextField inputDate2;
@FXML
TextField inputDate3;
@FXML
RadioButton inputIntake;
@FXML
RadioButton inputDigital;
@FXML
RadioButton inputPaid;
//endregion

//region @FXML Objects viewProject
@FXML
Spinner<String> spinnerProjectSelect;
@FXML
TextField textFieldProjectSpent;
@FXML
TextField textFieldProjectGained;
@FXML
TextField textFieldProjectRevenue;
@FXML
ScrollPane scrollPane;

//region @FXML TableView and Columns
@FXML
TableView<Bill> tableView;
@FXML
TableColumn<Bill, String> columnProject;
@FXML
TableColumn<Bill, String> columnAmount;
@FXML
TableColumn<Bill, String> columnIntake;
@FXML
TableColumn<Bill, String> columnDigital;
@FXML
TableColumn<Bill, String> columnShop;
@FXML
TableColumn<Bill, String> columnReason;
@FXML
TableColumn<Bill, String> columnPerson;
@FXML
TableColumn<Bill, String> columnDate1;
@FXML
TableColumn<Bill, String> columnDate2;
@FXML
TableColumn<Bill, String> columnDate3;
@FXML
TableColumn<Bill, String> columnPaid;


public void addTableViewData(ActionEvent event){
columnProject.setCellValueFactory(new PropertyValueFactory<Bill, String>("project"));
tableView.setItems(getObservableBills());
}

private ObservableList<Bill> getObservableBills(){
return FXCollections.observableArrayList(app.getBills());
}

public Controller() throws IOException {
app = new App();
dataManager = new DataManager();

}

}

fxml文件(只包含按钮和TableView):

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>

<TableView fx:id="tableView" prefHeight="585.0" prefWidth="800.0">
<columns>
<TableColumn fx:id="columnProject" prefWidth="94.0" text="Project" />
<TableColumn fx:id="columnAmount" prefWidth="66.0" text="Amount" />
<TableColumn fx:id="columnIntake" prefWidth="57.0" text="Intake" />
<TableColumn fx:id="columnDigital" minWidth="0.0" prefWidth="54.0" text="Digital" />
<TableColumn fx:id="columnShop" minWidth="1.0" prefWidth="85.0" text="Shop" />
<TableColumn fx:id="columnReason" prefWidth="86.0" text="Reason" />
<TableColumn fx:id="columnPerson" prefWidth="87.0" text="Person" />
<TableColumn fx:id="columnDate1" prefWidth="74.0" text="Date 1" />
<TableColumn fx:id="columnDate2" prefWidth="64.0" text="Date 2" />
<TableColumn fx:id="columnDate3" prefWidth="64.0" text="Date 3" />
<TableColumn fx:id="columnPaid" prefWidth="68.0" text="Paid" />
</columns>
</TableView>

<Button fx:id="buttonReload" mnemonicParsing="false" onAction="#addTableViewData" prefHeight="28.0" prefWidth="140.0" text="Reload" GridPane.rowIndex="2" />

模块信息.java

module AutomatedFinances {
requires javafx.controls;
requires javafx.fxml;
requires opencsv;

opens net.strobl.main;
}

最佳答案

在你的 Bill 类中你有:

public class Bill {

private String project;

// getter
public String getProject() {
return project;
}

// setter
public void setProject(String project) {
this.project = project;
}

// ????
public String projectProperty() {
return project;
}

}

当您对 TableColumn 单元格值工厂使用回调 new PropertyValueFactory("project") 时,当评估 call 方法时会发生以下情况:

@Override 
public ObservableValue<T> call(CellDataFeatures<S,T> param) {
return getCellDataReflectively(param.getValue());
}

private ObservableValue<T> getCellDataReflectively(S rowData) {
...

if (propertyRef.hasProperty()) {
return propertyRef.getProperty(rowData);
} else {
T value = propertyRef.get(rowData);
return new ReadOnlyObjectWrapper<T>(value);
}
...
}

首先尝试使用 JavaFX 属性,在 PropertyReference 中您可以看到:

public boolean hasProperty() {
reflect();
return propertyGetter != null;
}

由于您提供了 projectProperty() 方法,因此该方法被错误地视为 propertyGetter,因为它在 中以 Property 结尾>反射():

// Now attempt to look for the property-getter.
final String propertyGetterName = name + "Property";

因此它将返回 true,然后 getProperty() 将尝试转换此属性:

try {
return (ReadOnlyProperty<T>)MethodHelper.invoke(propertyGetter, bean, (Object[])null);
}

这将抛出您得到的异常:

Caused by: java.lang.ClassCastException: class java.lang.String cannot be cast to class javafx.beans.property.ReadOnlyProperty (java.lang.String is in module java.base of loader 'bootstrap'; javafx.beans.property.ReadOnlyProperty is in module javafx.base of loader 'app')
at javafx.base/com.sun.javafx.property.PropertyReference.getProperty(PropertyReference.java:197)

因为您的 String 项目 显然不能转换为 ReadOnlyProperty

解决方案

您要么使用适当的属性:

private final StringProperty project = new SimpleStringProperty();

public final StringProperty projectProperty() {
return project;
}

public final String getProject() {
return project.get();
}

public final void setProject(String value) {
project.set(value);
}

或者您删除了错误的属性 getter,然后回调将默认为第二种情况,使用 getCellDataReflectively 中的 String 基元,这将被转换为只读属性:

T value = propertyRef.get(rowData);
return new ReadOnlyObjectWrapper<T>(value);

虽然这工作正常,但首选第一个选项,使用适当的 JavaFX 属性。

关于JavaFX PropertyValueFactory 无法检索属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55874952/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com