Javafx tableview 反射不工作

标签 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 = 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

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

我得到的异常是:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.IllegalAccessException: Class sun.reflect.misc.Trampoline can not access a member of class SensorTableEntry with modifiers "public" at com.sun.javafx.property.PropertyReference.getProperty(PropertyReference.java:200)

最佳答案

您的属性必须是完全可访问的,因此它们的 getter 和它们的所有者类必须都是公共(public)的

所以简单地替换这个:

class SensorTableEntry {

有了这个:

public class SensorTableEntry {

关于Javafx tableview 反射不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40591823/

相关文章:

Java 串行通信 API - 如果发生超时,inputstream.read() 返回什么?

java - 将 Material 持久底板高度设置为屏幕高度的一半?

java - 将 JavaFX jmod 添加到 java --list-modules

java - 在 JavaFX 中显示复杂的印度孟加拉语字体

java - 如何在 tableview 标题上添加菜单按钮

java - 使用java设置Mac防火墙

java - 如何在 Java 中创建 “FTPS” 模拟服务器以进行单元测试,出现错误 - javax.net.ssl.SSLException : 502 Command not implemented: AUTH

java - 在 Java EE 应用程序中使用 Thread.sleep()、定时器或平台 cron 作业进行定期操作

检测到 JavaFX 未完成的资源锁

java - Java中从RGB到HSV(Color.RGBtoHSB)返回不同的结果