1. 程式人生 > >javafx error --- Can not retrieve property 'xxx' in PropertyValueFactory

javafx error --- Can not retrieve property 'xxx' in PropertyValueFactory

在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.Prope[email protected] 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 類才能進行繫結?