2015-10-20 39 views
0

你好,我的问题是在下面我有一个特定类的列表,我想将这些值绑定到一个jtable(它们被添加的顺序现在不重要) 类是在列表中是folows从列表中添加值<Class>到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; 
} 

所以现在我想所有这些高分添加到我的Jtable1其中驻留在jpannel。最简单/最有效的方法是什么

+0

可能重复[填充JTable使用列表](http://stackoverflow.com/questions/11095802/populate-jtable-using-list) –

回答

1

如果您必须创建一个JTable对象,最简单的方法是使用它的一个构造函数。

有两个JTable的构造函数直接接收数据(SimpleTableDemo使用第一):

JTable(Object[][] rowData, Object[] columnNames) 
JTable(Vector rowData, Vector columnNames) 

rowData必须在你想他们要显示的位置,你的对象。例如,要获取第二行第一列中的对象,JTable将执行rowData[1][0](其格式为rowData[row][col])。

1

您应该创建一个自定义的TableModel来保存您的HighScore对象。

结账Row Table Model。它会告诉你如何可以:

  1. 从头
  2. 使用一个通用的TableModel创建一个自定义的TableModel,只是实现了几个方法来反映你的高分对象的数据。