[英]Adding values from a List<Class> to Jtable
您好,我的問題是跟隨我有一個特定類的列表,我想將這些值綁定到一個jtable(現在添加它們的順序並不重要),列表中的類如下
public HighScore(int score, int amountofplayers, String playerName,String dateString) {
this.score = score;
this.amountofplayers = amountofplayers;
this.playerName = playerName;
this.dateString = dateString;
}
返回列表的函數是
public ArrayList<HighScore> GetHighScores()
{
ArrayList<HighScore> highscores = new ArrayList<HighScore>();
//get highscores from databse
//insert some test values
highscores.add(new HighScore(125, 2, "Piet","20-10-2015"));
highscores.add(new HighScore(167, 2, "Henk", "19-10-2015"));
highscores.add(new HighScore(278, 2, "Jan", "11-10-2015"));
return highscores;
}
因此,現在我想將所有這些高分添加到駐留在jpannel中的Jtable1中。 最簡單/最有效的方法是什么
如果必須創建JTable對象,最簡單的方法是使用其構造函數之一。
有兩個直接接受數據的JTable構造函數( SimpleTableDemo
使用第一個):
JTable(Object[][] rowData, Object[] columnNames)
JTable(Vector rowData, Vector columnNames)
rowData
必須使對象處於希望顯示它們的位置。 例如,要獲取第二行第一列中的對象,JTable將執行rowData[1][0]
(遵循rowData[row][col]
)。
您應該創建一個自定義TableModel
來保存您的HighScore對象。
查看行表模型 。 它將向您展示如何:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.