我正在尝试从 fxml 文件填充示例 javafx TableView。
这是我的 Controller 方法:
public class TestController implements Initializable {
@FXML private TableView<user> tableView;
@FXML private TableColumn<user, String> UserId;
@FXML private TableColumn<user, String> UserName;
public void initialize(URL location, ResourceBundle resources) {
UserId.setCellValueFactory(new PropertyValueFactory<user, String>("userId"));
UserName.setCellValueFactory(new PropertyValueFactory<user, String>("userName"));
tableView.getItems().setAll(parseUserList());
}
private List<user> parseUserList(){
List<user> l_u = new ArrayList<user>();
user u = new user();
u.setUserId(1);
u.setUserName("test1");
l_u.add(u);
u.setUserId(2);
u.setUserName("test2");
l_u.add(u);
u.setUserId(3);
u.setUserName("test3");
l_u.add(u);
return l_u;
}
}
和 fxml 文件:
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="369.0" prefWidth="505.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="viewmodel.TestController ">
<center>
<TableView fx:id="tableView" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<columns>
<TableColumn prefWidth="75.0" text="UserId" fx:id="UserId"/>
<TableColumn prefWidth="75.0" text="UserName" fx:id="UserName"/>
</columns>
</TableView>
</center>
</BorderPane>
最后是我的模型:
封装模型;
public class user {
private int userId;
public int getUserId() { return this.userId; }
public void setUserId(int userId) { this.userId = userId; }
private String userName;
public String getUserName() { return this.userName; }
public void setUserName(String userName) { this.userName = userName; }
}
现在,当我尝试填充它时,出现了这个错误:
Jun 28, 2019 12:17:35 PM javafx.scene.control.cell.PropertyValueFactory getCellDataReflectively
WARNING: Can not retrieve property 'userId' in PropertyValueFactory: javafx.scene.control.cell.PropertyValueFactory@638db851 with provided class type: class model.user
java.lang.RuntimeException: java.lang.IllegalAccessException: module javafx.base cannot access class model.user (in module JavaFXTest) because module JavaFXTest does not open model to javafx.base
at javafx.base/com.sun.javafx.property.PropertyReference.get(PropertyReference.java:176)
SO 上的一些文章提到,getter 和 setter 属性必须在 get word 之后以大写开头,但这并没有解决问题。
最佳答案
虽然违反 java 命名约定(始终遵循它们!)和 PropertyValueFactory 的不当使用(如果没有令人信服的理由,请不要使用它!)是常见的嫌疑人,但它们似乎不是 OP 中的原因问题。
错误消息表明问题出现在模块化上下文中:
Jun 28, 2019 12:17:35 PM javafx.scene.control.cell.PropertyValueFactory getCellDataReflectively
WARNING: Can not retrieve property 'userId' in PropertyValueFactory:
javafx.scene.control.cell.PropertyValueFactory@638db851 with provided class type: class model.user
java.lang.RuntimeException: java.lang.IllegalAccessException:
module javafx.base cannot access class model.user (in module JavaFXTest)
because module JavaFXTest does not open model to javafx.base
实际上,该示例在非模块化上下文中运行得很好,但在模块化时会失败。错误消息准确地告诉我们该怎么做:打开我们的模块(或其中的一部分)进行反射访问。
打开完整模块或单个包的模块信息:
// complete
open module JavaFXTest {
exports ourpackage;
...
}
// parts
module JavaFXTest {
exports ourpackage;
opens ourpackage.model;
}
关于java - PropertyValueFactory 模型类中正确的 getter 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56803049/