Java 从列表中添加值<;类别>;到Jtable
您好,我的问题是如下所示,我有一个特定类的列表,我想将其中的值绑定到jtable(它们的添加顺序现在并不重要) 列表中的类如下所示Java 从列表中添加值<;类别>;到Jtable,java,swing,arraylist,jtable,Java,Swing,Arraylist,Jtable,您好,我的问题是如下所示,我有一个特定类的列表,我想将其中的值绑定到jtable(它们的添加顺序现在并不重要) 列表中的类如下所示 public HighScore(int score, int amountofplayers, String playerName,String dateString) { this.score = score; this.amountofplayers = amountofplayers; this.playerName = player
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;
}
public ArrayList GetHighScores()
{
ArrayList highscores=新建ArrayList();
//从数据库中获得高分
//插入一些测试值
高分。增加(新的高分(125,2,“Piet”,“20-10-2015”);
高分。添加(新高分(167,2,“Henk”,“19-10-2015”);
高分。增加(新的高分(278,2,“2015年1月11-10日”);
返回高分;
}
所以现在我想将所有这些高分添加到驻留在jpannel中的Jtable1中。最简单/最有效的方法是什么?如果必须创建JTable对象,最简单的方法是使用其构造函数之一 有两个JTable构造函数直接接受数据(
SimpleTableDemo
使用第一个):
rowData
必须将对象放置在希望显示它们的位置。例如,要获取第二行第一列中的对象,JTable将执行rowData[1][0]
(它遵循rowData[row][col]
)的格式。您应该创建一个自定义TableModel
来保存高分对象
退房。它将向您展示如何:
您应该使用DefautTableModel,创建它的实例。然后使用for循环将高分传递给对象[],然后使用DefautTableModel addRow方法添加行 检查C12-H22-O11答案,以获取可能重复的示例
JTable(Object[][] rowData, Object[] columnNames)
JTable(Vector rowData, Vector columnNames)