Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从列表中添加值<;类别>;到Jtable_Java_Swing_Arraylist_Jtable - Fatal编程技术网

Java 从列表中添加值<;类别>;到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

您好,我的问题是如下所示,我有一个特定类的列表,我想将其中的值绑定到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;
}
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
来保存高分对象

退房。它将向您展示如何:

  • 从头开始创建自定义表格模型
  • 使用一个通用的TableModel,只需实现几个方法来反映HighScore对象的数据

  • 您应该使用DefautTableModel,创建它的实例。然后使用for循环将高分传递给对象[],然后使用DefautTableModel addRow方法添加行

    检查C12-H22-O11答案,以获取可能重复的示例
    JTable(Object[][] rowData, Object[] columnNames)
    JTable(Vector rowData, Vector columnNames)