gpt4 book ai didi

JavaFX setCellValueFactory 无法检索属性 IllegalAcessException

转载 作者:行者123 更新时间:2023-12-02 08:42:11 24 4
gpt4 key购买 nike

我已经检查了至少 20 个与该主题相关的其他线程,并尝试尽我所能进行调试,但我不知道为什么它不起作用。我希望我的“addButton”在按下按钮时将数据从文本框添加到我的 TableView 中。

我的数据类:

package org.application;

import javafx.beans.property.SimpleStringProperty;

public class Credits {
private SimpleStringProperty title;
private SimpleStringProperty occupation;
private SimpleStringProperty person;

public Credits (String title, String occupation, String person){
this.title = new SimpleStringProperty(title);
this.occupation = new SimpleStringProperty(occupation);
this.person = new SimpleStringProperty(person);
}

public String getTitle() {
return this.title.get();
}

public void setTitle(String title) {
this.title.set(title);
}

public String getOccupation() {
return this.occupation.get();
}

public void setOccupation(String occupation) {
this.occupation.set(occupation);
}

public String getPerson() {
return this.person.get();
}

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

}

我的 Controller 类重要部分的片段

    public Button searchButton;
public Button addButton;
public Button deleteButton;
public Button updateButton;
public TextField searchBox;
public TextField setTitleBox;
public TextField setOccupationBox;
public TextField setPersonBox;
public TableView creditTable;
public TableColumn title;
public TableColumn occupation;
public TableColumn person;
public ListView test;

final ObservableList<Credits> credits = FXCollections.observableArrayList();

public void initialize() {
title.setCellValueFactory(new PropertyValueFactory<Credits, String>("title"));
occupation.setCellValueFactory(new PropertyValueFactory<Credits, String>("occupation"));
person.setCellValueFactory(new PropertyValueFactory<Credits, String>("person"));
}

public void addButton(ActionEvent event) {
String title = setTitleBox.getText();
String occupation = setOccupationBox.getText();
String person = setPersonBox.getText();
credits.add(new Credits(title, occupation, person));
creditTable.setItems(credits);
}

fxml 文件:

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" styleClass="rightSide" stylesheets="@../../ECMS.css" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.presentation.MainMenu">
<children>
<Button fx:id="loginButton" layoutX="14.0" layoutY="14.0" mnemonicParsing="false" onAction="#switchToLoginScreen" styleClass="loginButton" text="Login" />
<TextField fx:id="searchBox" layoutX="14.0" layoutY="108.0" prefHeight="30.0" prefWidth="195.0" promptText="Name" />
<Button fx:id="searchButton" layoutX="214.0" layoutY="108.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="100.0" styleClass="loginButton" text="Search" />
<Button fx:id="helpButton" layoutX="63.0" layoutY="14.0" mnemonicParsing="false" onAction="#switchToHelpPopup" styleClass="loginButton" text="Help" />
<Label layoutX="14.0" layoutY="86.0" prefHeight="17.0" prefWidth="162.0" text="Search for productions:">

<font>
<Font name="Ebrima Bold" size="14.0" />
</font></Label>
<ListView fx:id="test" layoutX="14.0" layoutY="143.0" prefHeight="450.0" prefWidth="300.0" />
<Button fx:id="closeButton" layoutX="775.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#closeButtonAction" prefHeight="20.0" prefWidth="25.0" styleClass="closeButton" text="X">
<font>
<Font name="Eras Bold ITC" size="12.0" />
</font></Button>
<Button fx:id="addButton" layoutX="363.0" layoutY="93.0" mnemonicParsing="false" onAction="#addButton" prefHeight="31.0" prefWidth="100.0" text="Add" />
<Button fx:id="deleteButton" layoutX="504.0" layoutY="93.0" mnemonicParsing="false" prefHeight="31.0" prefWidth="100.0" text="Delete" />
<Button fx:id="updateButton" layoutX="652.0" layoutY="93.0" mnemonicParsing="false" prefHeight="31.0" prefWidth="100.0" text="Update" />
<TableView fx:id="creditTable" editable="true" layoutX="332.0" layoutY="143.0" prefHeight="450.0" prefWidth="450.0">
<columns>
<TableColumn fx:id="title" prefWidth="147.0" text="Title" />
<TableColumn fx:id="occupation" minWidth="0.0" prefWidth="132.0" text="Occupation" />
<TableColumn fx:id="person" minWidth="-1.0" prefWidth="170.0" text="Person" />
</columns>
</TableView>
<TextField fx:id="setPersonBox" layoutX="634.0" layoutY="54.0" prefHeight="30.0" prefWidth="136.0" promptText="Person" />
<Label layoutX="500.0" layoutY="30.0" prefHeight="18.0" prefWidth="114.0" text="Add production:">
<font>
<Font name="Ebrima Bold" size="14.0" />
</font>
</Label>
<TextField fx:id="setOccupationBox" layoutX="489.0" layoutY="54.0" prefHeight="30.0" prefWidth="136.0" promptText="Occupation" />
<TextField fx:id="setTitleBox" layoutX="345.0" layoutY="54.0" prefHeight="30.0" prefWidth="136.0" promptText="Title" />
</children>
</AnchorPane>

错误:(它对 3 个 CellProperty 行中的每一个都会出现此错误,这只是 1 个,但它们几乎都是相同的)。

Apr 19, 2020 7:20:12 PM javafx.scene.control.cell.PropertyValueFactory getCellDataReflectively
WARNING: Can not retrieve property 'title' in PropertyValueFactory: javafx.scene.control.cell.PropertyValueFactory@210cabaf with provided class type: class org.application.Credits
java.lang.RuntimeException: java.lang.IllegalAccessException: module javafx.base cannot access class org.application.Credits (in module org.example) because module org.example does not open org.application to javafx.base
at javafx.base/com.sun.javafx.property.PropertyReference.get(PropertyReference.java:176)
at javafx.controls/javafx.scene.control.cell.PropertyValueFactory.getCellDataReflectively(PropertyValueFactory.java:184)
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$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:9540)
at javafx.graphics/javafx.scene.Node.applyCss(Node.java:9627)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.setCellIndex(VirtualFlow.java:1749)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1726)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.getCellLength(VirtualFlow.java:1852)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.computeViewportOffset(VirtualFlow.java:2755)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1245)
at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1206)
at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1213)
at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1213)
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(AccessController.java:391)
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:563)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:543)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:536)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$11(QuantumToolkit.java:342)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.IllegalAccessException: module javafx.base cannot access class org.application.Credits (in module org.example) because module org.example does not open org.application to javafx.base
at javafx.base/com.sun.javafx.property.MethodHelper.invoke(MethodHelper.java:69)
at javafx.base/com.sun.javafx.property.PropertyReference.get(PropertyReference.java:174)
... 40 more

最佳答案

出现异常的原因是 PropertyValueFactory 使用反射来访问模型类 Credits 中的方法调用。由于您的模块未打开以进行对 javafx.base 模块的此类访问,因此您会收到您发布的 IllegalAccessException

您可以通过在 module-info.java 文件中添加如下行来解决此问题:

opens org.application to javafx.base ;

也许更好的方法是避免使用 PropertyValueFactory。该类的设计确实是因为,在 Java 8 和 lambda 表达式之前,仅仅为了访问模型类中的属性而创建 CellValueFactory 是相当麻烦的。随着 Java 8 中 lambda 表达式的出现,情况不再如此,并且使用 lambda 表达式代替反射式 PropertyValueFactory 可能会提供更好的性能,更重要的是,提供编译时检查。

首先(无论如何您都应该这样做),将属性访问器方法添加到您的 Credits 类中。按照您的类当前的情况,您创建了 StringProperty 实例,但没有方法来访问它们(只有访问其内容的方法)。因此,如果您调用,例如,您的表将不会动态更新在已显示的 Credits 实例上使用 setTitle()。您只需要添加

public class Credits {

// all existing code...

public StringProperty titleProperty() {
return title ;
}

public StringProperty occupationProperty() {
return occupation ;
}

public StringProperty personProperty() {
return person ;
}

}

现在您可以直接在 Credits 类中引用属性:

title.setCellValueFactory(cellData -> cellData.getValue().titleProperty());

其他列也类似。除了其他好处(主要是编译时检查属性是否实际存在)之外,这还避免了将 Credits 类反射性地暴露给 javafx.base 模块的需要.

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

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