Reputation: 37
I am trying to take the contents inside my ArrayList, holding FootballClub objects and display them onto my JTable. I can't seem to get this to work and I am not sure what I am doing wrong. Any help would be greatly appreciated. It says for model.add() that an array initializer is not allowed here. My columnNames also seem to not be displaying
// the arraylist containing footballclub objects
protected ArrayList<FootballClub> clubs = new ArrayList<FootballClub>();
public void displayTable(ArrayList<FootballClub> footballClubs)
{
String[] columnNames = {"Club name", "goals", "points", "wins"};
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
for(int i = 0; i < footballClubs.size(); i++)
{
String name = footballClubs.get(i).getClubName();
int goals = footballClubs.get(i).getGoals();
int points = footballClubs.get(i).getPoints();
int wins = footballClubs.get(i).getPoints();
model.addRow({{name, goals,points,wins}});
}
final JTable teamTable = new JTable(model);
teamTable.setFillsViewportHeight(true);
JFrame frame = new JFrame("Tableview");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setSize(500, 500);
frame.setVisible(true);
}
Upvotes: 1
Views: 111
Reputation: 147164
You don't say how what you've written doesn't work. I assume there is a compiler issue on this line:
model.addRow({{name, goals,points,wins}});
It looks as if you are trying to use the Object[]
overload. The correct syntax is:
model.addRow(new Object[] { name, goals, points, wins });
Or, the special syntax for array initialisers:
Object[] row = { name, goals, points, wins} ;
model.addRow(row);
If there had been a List
overload, you could use List.of(name, goals, points, wins)
, but there isn't.
(Also note, it is conventional to use List
instead of ArrayList
. If there is a conflict with java.awt.List
you can explicitly add import java.util.List
.
The for
can be written:
for (FootballClub club : footballClubs) {
which should make things clearer.)
Upvotes: 2