我试图用模拟数据填充JavaFx TableView列,但我不断收到反射错误,即使我认为我正在遵循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<SensorTableEntry> sensorEntries = FXCollections.observableArrayList(
new SensorTableEntry(1, "manufacturer", "type", "00:00:00:00:00:00")
);
TableView<SensorTableEntry> table = new TableView<SensorTableEntry>();
TableColumn<SensorTableEntry,Integer> idCol = new TableColumn<SensorTableEntry,Integer>("ID");
idCol.setCellValueFactory(new PropertyValueFactory<SensorTableEntry,Integer>("id"));
TableColumn<SensorTableEntry,String> manufacturerCol = new TableColumn<SensorTableEntry,String>("Manufacturer");
manufacturerCol.setCellValueFactory(new PropertyValueFactory<SensorTableEntry,String>("manufacturer"));
TableColumn<SensorTableEntry,String> typeCol = new TableColumn<SensorTableEntry,String>("Type");
typeCol.setCellValueFactory(new PropertyValueFactory<SensorTableEntry,String>("type"));
TableColumn<SensorTableEntry,String> btAddressCol = new TableColumn<SensorTableEntry,String>("Bluetooth Address");
btAddressCol.setCellValueFactory(new PropertyValueFactory<SensorTableEntry,String>("btAddress"));
table.setItems(sensorEntries);
table.getColumns().addAll(
idCol,
manufacturerCol,
typeCol,
btAddressCol
);
pane.getChildren().add(table);
我检查过类似问题的其他答案,例如:
Javafx PropertyValueFactory not populating Tableview
JavaFx TableView not filling all required columns
Javafx tableview not showing data in all columns
但无论我检查多少,我似乎都没有找到我的命名出错的地方 . 我错过了什么吗?
我得到的例外是:
线程“JavaFX Application Thread”中的异常java.lang.RuntimeException:java.lang.IllegalAccessException:类sun.reflect.misc.Trampoline无法访问com.Sun.javafx.property中带有修饰符“public”的SensorTableEntry类的成员.PropertyReference.getProperty(PropertyReference.java:200)
2 回答
您的属性必须完全可访问,以便他们的getter和他们的所有者类 must both be public .
So simply replace this:
With this:
由于您在模型中使用JavaFX属性,因此可以使用回调的实际实现(为简洁起见使用lambda表达式)并完全避免反射 . 请注意
IntegerProperty
实现Property<Number>
,而不是Property<Integer>
,因此您需要修复类型(请参阅JavaFX Properties in TableView):这通常是一种更好的方法:编译器将检查属性是否存在且类型是否正确,并且由于您不依赖于反射来评估单元格值,因此性能会更好(可能可以忽略不计,但仍然...... ) .
另外一个:在JavaFX属性模式中,原始包装器属性的方法应该使用基本类型,而不是对象包装器类型,即: