版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Miha_Singh/article/details/83687123
在javafx中使用表格时,将TableColumn与自己创建的类的某个字段绑定时出现的问题,自建类定义如下:
class RankRecord{
private final SimpleIntegerProperty rankNumber = new SimpleIntegerProperty();
private final SimpleStringProperty playerName = new SimpleStringProperty();
private final SimpleIntegerProperty score = new SimpleIntegerProperty();
public RankRecord(int ranknum, String playername, int score) {
this.setRankNumber(ranknum);
this.setPlayerName(playername);
this.setScore(score);
}
public void setRankNumber(int ranknum) {
this.rankNumber.set(ranknum);
}
public void setPlayerName(String name) {
this.playerName.set(name);
}
public void setScore(int score) {
this.score.set(score);
}
public int getRankNumber() {
return this.rankNumber.get();
}
public String getPlayeName() {
return this.playerName.get();
}
public int getScore() {
return this.score.get();
}
public SimpleIntegerProperty rankNumberProperty() {
return this.rankNumber;
}
public SimpleStringProperty playerNameProperty() {
return this.playerName;
}
public SimpleIntegerProperty scoreProperty() {
return this.score;
}
}
报错的信息如下:
Can not retrieve property 'playerName' in PropertyValueFactory: javafx.scene.control.cell.PropertyValueFactory@2e6ad73b with provided class type: class crossword.RankRecord
java.lang.RuntimeException: java.lang.IllegalAccessException: class com.sun.javafx.reflect.Trampoline cannot access a member of class crossword.RankRecord with modifiers "public"
但是很明显我的类里是由那些字段的,可是为什么不能绑定呢?
还有就是说我的类的成员的限定符是public呢,明明是private啊!
后来发现是自建的类的限定符的问题,把自己的类定义为public 类就可以了,
public class RankRecord{...}
虽然问题解决了,但是新的问题又来了:
如果不指定修饰符的话,则默认为只能被同包内的类访问,经过排查代码,发现对TableColumn进行数据绑定的代码在另一个类中,而且这个类也没加修饰,可是这些代码都在同一个包中啊,应该可以访问到啊!!!根据 java.lang.RuntimeException: java.lang.IllegalAccessException
这个错误提示,难道是必须要定义成public 类才能进行绑定?