Javafx tableview反射不工作
我试图用模拟数据填充JavaFx TableView列,但我一直得到一个反射错误,尽管我认为我正确地遵循了Bean约定:Javafx tableview反射不工作,java,javafx,Java,Javafx,我试图用模拟数据填充JavaFx TableView列,但我一直得到一个反射错误,尽管我认为我正确地遵循了Bean约定: // Data model class SensorTableEntry { SensorTableEntry(Integer id, String man, String type, String addr) { this.id = new SimpleIntegerProperty(id); this.manufacturer =
// 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);
//数据模型
类表条目{
SensorTableEntry(整数id、字符串管理器、字符串类型、字符串地址){
this.id=新的SimpleIntegerProperty(id);
this.manufacturer=新的SimpleStringProperty(man);
this.type=新的SimpleStringProperty(类型);
this.btAddress=新的SimpleStringProperty(addr);
}
私有IntegerProperty id;
公共整数getId(){return idProperty().get();}
public void setId(整数值){idProperty().set(值);}
公共IntegerProperty idProperty(){return id;}
私人物业制造商;
public void setManufacturer(字符串值){manufacturerProperty().set(值);}
公共字符串getManufacturer(){return manufacturerProperty().get();}
public StringProperty manufacturerProperty(){return manufacturer;}
私有财产类型;
public void setType(字符串值){typeProperty().set(值);}
公共字符串getType(){return typeProperty().get();}
public StringProperty typeProperty(){return type;}
私人地址;
公共无效设置地址(字符串值){btAddressProperty().set(值);}
公共字符串getBtAddress(){return btAddressProperty().get();}
public StringProperty btAddressProperty(){return btAddress;}
}
//在此之前有更多代码。。。
//控制器内的实际表格
ObservableList sensorEntries=FXCollections.observableArrayList(
新的传感器表条目(1,“制造商”、“类型”、“00:00:00:00:00”)
);
TableView table=新TableView();
TableColumn idCol=新的TableColumn(“ID”);
idCol.setCellValueFactory(新属性ValueFactory(“id”);
TableColumn manufacturerCol=新的TableColumn(“制造商”);
manufacturerCol.setCellValueFactory(新属性ValueFactory(“制造商”));
TableColumn typeCol=新的TableColumn(“类型”);
typeCol.setCellValueFactory(新属性ValueFactory(“类型”);
TableColumn btAddressCol=新的TableColumn(“蓝牙地址”);
btAddressCol.setCellValueFactory(新属性值工厂(“btAddress”);
表.集合项目(传感器条目);
table.getColumns().addAll(
艾德科尔,
制造业,
typeCol,
btAddressCol
);
pane.getChildren().add(表);
我检查了类似问题的其他答案,如:
但无论我检查了多少,我似乎都没有发现我的命名哪里出了问题。我错过什么了吗
我得到的例外是:
线程“JavaFX应用程序线程”java.lang.RuntimeException:java.lang.IllegalacessException:Class sun.reflect.misc.Trampoline无法访问修饰符为“public”的SensorTableEntry类的成员
位于com.sun.javafx.property.PropertyReference.getProperty(PropertyReference.java:200)
由于您在模型中使用JavaFX属性,所以可以使用回调的实际实现(为了简洁起见,使用lambda表达式)并完全避免反射。请注意,
IntegerProperty
实现的是Property
,而不是Property
,因此需要修复类型(请参阅):
由于您在模型中使用JavaFX属性,所以可以使用回调的实际实现(为了简洁起见,使用lambda表达式)并完全避免反射。请注意,
IntegerProperty
实现的是Property
,而不是Property
,因此需要修复类型(请参阅):
您的属性必须是完全可访问的,因此它们的getter和所有者类都必须是公共的 因此,只需将其替换为:
class SensorTableEntry {
public class SensorTableEntry {
使用此选项:
class SensorTableEntry {
public class SensorTableEntry {
您的属性必须是完全可访问的,因此它们的getter和所有者类都必须是公共的 因此,只需将其替换为:
class SensorTableEntry {
public class SensorTableEntry {
使用此选项:
class SensorTableEntry {
public class SensorTableEntry {
尝试将您的bean类
SensorTableEntry
转换为public
类,而不是包保护的类。此操作成功,谢谢!我知道我犯了一些愚蠢的错误尝试将您的bean类SensorTableEntry
转换为public
类,而不是包保护。这很有效,谢谢!我知道我犯了一些愚蠢的错误,这也行得通,但我不得不改变一下:TableColumn(“ID”);idCol.setCellValueFactory(cellData->new SimpleIntegerProperty(cellData.getValue().idProperty().getValue()).asObject())@如果将列设置为可编辑的,则FedericoOrquera将无法正常工作(即使不可编辑,继续创建这样的新属性也是低效的)。正如我在回答中指出的,您应该将列的类型从Integer
更改为Number
。再一次,看到了。这也起作用了,但我不得不这样修改它:TableColumn(“ID”);idCol.setCellValueFactory(cellData->new SimpleIntegerProperty(cellData.getValue().idProperty().getValue()).asObject())@如果将列设置为可编辑的,则FedericoOrquera将无法正常工作(即使不可编辑,继续创建这样的新属性也是低效的)。正如我在回答中指出的,您应该将列的类型从Integer
更改为Number
。再一次,看。