Reputation: 31
Hello my problem is the foloowing i have a List of a certain class of which i want to bind the values to a jtable (order in which they are added isnt important right now) The class that is in the list is as folows
public HighScore(int score, int amountofplayers, String playerName,String dateString) {
this.score = score;
this.amountofplayers = amountofplayers;
this.playerName = playerName;
this.dateString = dateString;
}
the function that returns the list is
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;
}
So now i want to add all these highscores to my Jtable1 in which resides in a jpannel. What would be the easiest/most effiecient way to do this
Upvotes: 0
Views: 1311
Reputation: 1
you should use a DefautTableModel, creating a instance of it. Then use a for loop passing your highscores to an Object[], then adding rows with the DefautTableModel addRow method.
Check Populate JTable Using List C12-H22-O11 answer for an example
Upvotes: 0
Reputation: 324108
You should create a custom TableModel
to hold your HighScore objects.
Check out Row Table Model. It will show you how to either:
Upvotes: 1
Reputation: 10051
If you have to create a JTable object, the simplest way is to use one of its constructors.
There are two JTable constructors that directly accept data (SimpleTableDemo
uses the first):
JTable(Object[][] rowData, Object[] columnNames)
JTable(Vector rowData, Vector columnNames)
rowData
must have your objects in the position you want them to be shown. For instance, to obtain the object in the first column of the second row, JTable will execute rowData[1][0]
(it follows the form rowData[row][col]
).
Upvotes: 1