2016-11-14 5 views
1

私はモックデータをJavaFXのテーブルビューの列を埋めるためにしようとしていますが、私は私が私が正しく豆の規則を次のようだと思うにもかかわらず、反射エラーを得続ける:のJavaFXテーブルビュー反射

// 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のアプリケーションスレッド」java.lang.RuntimeException:java.lang.IllegalAccessExceptionが:クラスsun.reflect.misc.TrampolineがメンバーにアクセスすることはできませんあなたのモデルでのJavaFXプロパティを使用しているので、あなたが実際の実装を使用することができます

+1

は、Beanクラス 'SensorTableEntry'ではなく、パッケージの' public'クラスに変換しよう –

+0

これを保護しました働いて、ありがとう!私は何か愚かな間違いをしていることを知っていた –

答えて

1

com.sun.javafx.property.PropertyReference.getProperty(PropertyReference.java:200)で 「公共」の修飾子を持つクラスSensorTableEntryコールバック(簡潔にするためにラムダ式を使用)とリフレクションを避ける完全に。なお、IntegerProperty実装Property<Number>、ないProperty<Integer>、あなたはタイプを修正する必要がありますので、(JavaFX Properties in TableViewを参照してください):

TableColumn<SensorTableEntry,Number> idCol = new TableColumn<SensorTableEntry,Number>("ID"); 
idCol.setCellValueFactory(cellData -> cellData.getValue().idProperty()); 

TableColumn<SensorTableEntry,String> manufacturerCol = new TableColumn<SensorTableEntry,String>("Manufacturer"); 
manufacturerCol.setCellValueFactory(cellData -> cellData.getValue().manufacturerProperty()); 

TableColumn<SensorTableEntry,String> typeCol = new TableColumn<SensorTableEntry,String>("Type"); 
typeCol.setCellValueFactory(cellData -> cellData.getValue().typeProperty()); 

TableColumn<SensorTableEntry,String> btAddressCol = new TableColumn<SensorTableEntry,String>("Bluetooth Address"); 
btAddressCol.setCellValueFactory(cellData -> cellData.getValue().btAddressProperty()); 

これは、一般的にはるかに優れたアプローチです:コンパイラは、プロパティが存在し、正しいであることを確認しますセルの値を評価するためにリフレクションに頼っているわけではないので、パフォーマンスは向上します(恐らく無視されるかもしれませんが...)。 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... 
} 
+0

これはまた働いたが、私はこれのように少し変えなければならなかった: ** TableColumn ( "ID"); \t \t idCol.setCellValueFactory(cellData - > newIntegerProperty(cellData.getValue()。idProperty()。getValue())。asObject()); ** –

+0

@FedericoOrquera列を編集可能にすると正しく動作しません(そうでなくても、新しいプロパティを作成し続けるのは非効率的です)。私が答えて指摘したように、列の型を 'Integer'から' Number'に変更する必要があります。繰り返しますが、http://stackoverflow.com/questions/24889638/javafx-properties-in-tableview/24890425#24890425を参照してください。 –

+0

回答ありがとうございます! –

3

あなたの性質はそれほど彼らのゲッター完全にアクセス可能でなければならず、所有者クラスは、両方とも公開公開でなければなりません。

だから、単純にこの置き換えます。これにより

class SensorTableEntry { 

を:

public class SensorTableEntry {