【发布时间】:2021-03-01 15:59:07
【问题描述】:
我试图获取我的 ArrayList 中的内容,持有 FootballClub 对象并将它们显示到我的 JTable 上。我似乎无法让它工作,我不确定我做错了什么。任何帮助将不胜感激。它表示 model.add() 此处不允许使用数组初始值设定项。我的 columnNames 似乎也没有显示
// 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);
}
【问题讨论】:
标签: java swing arraylist jtable defaulttablemodel