Javafx tableviewreflection不起作用

我试图用模拟数据填充JavaFx TableView列,但我不断收到reflection错误,即使我认为我正在遵循Bean约定:

// Data model class SensorTableEntry { SensorTableEntry(Integer id, String man, String type, String addr) { this.id = new SimpleIntegerProperty(id); this.manufacturer = new SimpleStringProperty(man); this.type = new SimpleStringProperty(type); this.btAddress = new SimpleStringProperty(addr); } private IntegerProperty id; public Integer getId() { return idProperty().get(); } public void setId(Integer value) { idProperty().set(value); } public IntegerProperty idProperty() { return id; } private StringProperty manufacturer; public void setManufacturer(String value) { manufacturerProperty().set(value); } public String getManufacturer() { return manufacturerProperty().get(); } public StringProperty manufacturerProperty() { return manufacturer; } private StringProperty type; public void setType(String value) { typeProperty().set(value); } public String getType() { return typeProperty().get(); } public StringProperty typeProperty() { return type; } private StringProperty btAddress; public void setBtAddress(String value) { btAddressProperty().set(value); } public String getBtAddress() { return btAddressProperty().get(); } public StringProperty btAddressProperty() { return btAddress; } } // More code before this... // Actual table inside the controller ObservableList sensorEntries = FXCollections.observableArrayList( new SensorTableEntry(1, "manufacturer", "type", "00:00:00:00:00:00") ); TableView table = new TableView(); TableColumn idCol = new TableColumn("ID"); idCol.setCellValueFactory(new PropertyValueFactory("id")); TableColumn manufacturerCol = new TableColumn("Manufacturer"); manufacturerCol.setCellValueFactory(new PropertyValueFactory("manufacturer")); TableColumn typeCol = new TableColumn("Type"); typeCol.setCellValueFactory(new PropertyValueFactory("type")); TableColumn btAddressCol = new TableColumn("Bluetooth Address"); btAddressCol.setCellValueFactory(new PropertyValueFactory("btAddress")); table.setItems(sensorEntries); table.getColumns().addAll( idCol, manufacturerCol, typeCol, btAddressCol ); pane.getChildren().add(table); 

我检查过类似问题的其他答案,例如:

Javafx PropertyValueFactory没有填充Tableview

JavaFx TableView未填充所有必需的列

Javafx tableview没有显示所有列中的数据

但无论我检查多少,我似乎都没有找到我的命名出错的地方。 我错过了什么吗?

我得到的例外是:

线程“JavaFX Application Thread”中的exceptionjava.lang.RuntimeException:java.lang.IllegalAccessException:类sun.reflect.misc.Trampoline无法访问com.sun.javafx.property中带有修饰符“public”的SensorTableEntry类的成员。 PropertyReference.getProperty(PropertyReference.java:200)

您的财产必须完全可以访问,因此他们的获取者和所有者类必须都是公开的

所以简单地替换这个:

 class SensorTableEntry { 

有了这个:

 public class SensorTableEntry { 

由于您在模型中使用JavaFX属性,因此可以使用回调的实际实现(为简洁起见使用lambda表达式)并完全避免reflection。 请注意, IntegerProperty实现Property ,而不是Property ,因此您需要修复类型(请参阅TableView中的JavaFX属性 ):

 TableColumn idCol = new TableColumn("ID"); idCol.setCellValueFactory(cellData -> cellData.getValue().idProperty()); TableColumn manufacturerCol = new TableColumn("Manufacturer"); manufacturerCol.setCellValueFactory(cellData -> cellData.getValue().manufacturerProperty()); TableColumn typeCol = new TableColumn("Type"); typeCol.setCellValueFactory(cellData -> cellData.getValue().typeProperty()); TableColumn btAddressCol = new TableColumn("Bluetooth Address"); btAddressCol.setCellValueFactory(cellData -> cellData.getValue().btAddressProperty()); 

这通常是一种更好的方法:编译器将检查属性是否存在且类型是否正确,并且由于您不依赖于reflection来评估单元格值,因此性能会更好(可能可以忽略不计,但仍然…… )。

另外一个:在JavaFX属性模式中,原始包装器属性的方法应该使用基本类型,而不是对象包装器类型,即:

 class SensorTableEntry { SensorTableEntry(int id, String man, String type, String addr) { this.id = new SimpleIntegerProperty(id); this.manufacturer = new SimpleStringProperty(man); this.type = new SimpleStringProperty(type); this.btAddress = new SimpleStringProperty(addr); } private IntegerProperty id; public int getId() { return idProperty().get(); } public void setId(int value) { idProperty().set(value); } public IntegerProperty idProperty() { return id; } // existing code... }