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);
}
You don't say how what you've written doesn't work. I assume there is a compiler issue on this line:
It looks as if you are trying to use the
Object[]overload. The correct syntax is:Or, the special syntax for array initialisers:
If there had been a
Listoverload, you could useList.of(name, goals, points, wins), but there isn't.(Also note, it is conventional to use
Listinstead ofArrayList. If there is a conflict withjava.awt.Listyou can explicitly addimport java.util.List.The
forcan be written:which should make things clearer.)