[英]Java FXML Tableview with ArrayList
Hey guys I am trying to do a scorelist for a Jump&Run game for a project in university I am saving the data of a Player
class with the attributes nickname
and finalScore
in an ArrayList
. 嗨,大家好我尝试以跳跃和运行游戏,在大学我保存的数据的项目做一个得分列表
Player
与属性类的nickname
和finalScore
在ArrayList
。 I want to show the data in a FXML TableView
but it won't work and only shows no content. 我想在FXML
TableView
显示数据,但是它将不起作用,并且仅显示任何内容。
I have already tried to declare the attributes as SimpleString
/ IntegerProperty
s but it did not change. 我已经尝试将属性声明为
SimpleString
/ IntegerProperty
但是它没有改变。
public class ScoreController implements Initializable {
@FXML
private TableView<Player> table;
@FXML
private TableColumn<Player, String> Nickname;
@FXML
private TableColumn<Player, Integer> Score;
// Creating Observable Array List
private ObservableList<Player> data = FXCollections.observableArrayList();
// Set up the Scene
private Parent scoreList;
void setUp() throws Exception{
scoreList = FXMLLoader.load(getClass().getResource("/fxml/Score.fxml"));
App.scoreList = new Scene(scoreList,800,500);
}
// Adding data to the Observable List and setting Column Factories
@Override
public void initialize(URL url, ResourceBundle rb) {
data.add(new Player("Chris", 11));
data.add(new Player("Agil", 12));
Nickname.setCellValueFactory(new PropertyValueFactory<Player, String>("nickname"));
Score.setCellValueFactory(new PropertyValueFactory<Player, Integer>("finaleScore"));
table.setItems(data);
}
}
I expected the TableColumn to show the data of the ArrayList
but the TableView
only shows 'no content' in this table. 我希望TableColumn可以显示
ArrayList
的数据,但是TableView
在此表中仅显示“无内容”。
EDIT Player class 编辑播放器类
public Player(String nickname, int finalScore){
setNickname(nickname);
setFinalScore(finalScore);
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
void setFinalScore(int score){
finalScore = score;
}
public String getNickname(){
return nickname;
}
public int getFinalScore() {
return finalScore;
}
You could try some changes 您可以尝试一些更改
Nickname.setCellValueFactory(col ->new SimpleStringProperty(col.getValue().getNickName());
Score.setCellValueFactory(col ->new SimpleIntegerProperty(col.getValue().getFinalScore());
If its working, you have to look at the name convention for cellFactory
如果
cellFactory
,则必须查看cellFactory
的名称约定
I have tried out your code and this is what I can presume to be the issue: 我已经尝试了您的代码,这是我可以假定的问题:
Score.setCellValueFactory(new PropertyValueFactory<Player, Integer>("finaleScore"));
You have mistype finalScore
as finaleScore
, which returns no values since such a property does not exist. 您将
finalScore
输入为finaleScore
,但没有输入任何值,因为这样的属性不存在。 However, this does not affect population of the values for column Nickname. 但是,这不会影响“昵称”列的值填充。 So my second guess is that you may not have set the controller for the fxml file.
因此,我的第二个猜测是您可能尚未设置fxml文件的控制器。 Please check that in your Score.fxml code you have the line:
请检查您的Score.fxml代码中是否包含以下行:
fx:controller="<your package name here >.ScoreController"
eg 例如
<AnchorPane prefHeight="400.0" prefWidth="600.0" fx:controller="com.example.ScoreController">
<!-- other children nodes here -->
</AnchorPane>
EDIT 编辑
Try changing these lines to simply be: 尝试将这些行更改为:
@FXML
public TableView table;
@FXML
public TableColumn Nickname;
@FXML
public TableColumn Score;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.