JTable窗口未显示ArrayList内容



我正在尝试制作一个程序,在这个程序中,你可以启动一个带有按钮的摆动框架,然后从中选择一个按钮来做各种事情。其中之一就是显示一个包含ArrayList中数据的表。按下该按钮时,表将打开,但即使我将内容添加到数组列表中,内容也是空的。如果我以我在switch语句中注释掉的方式调用表;G";,我从数组列表中获取数据,表就可以工作了。

我不确定是什么原因导致了我无法按下打开表格和显示信息的按钮的问题。

public static void main(String[] args) {
/* Display user menu, select from menu */
String menuSelector;
Scanner selectfromMenu = new Scanner(System.in);
PremierLeagueManager PLM = new PremierLeagueManager();
PLM.menu();
/* dowhile to allow multiple functionality through menu */
do {
menuSelector = PLM.userInput.nextLine().toUpperCase();
switch(menuSelector){
case "A":
PLM.addClub();
break;
case "V":
PLM.displayClubs(PLM.clubs);
break;
case "D":
PLM.deleteClub(PLM.clubs);
break;
case "S":
PLM.addMatchStatistic(PLM.clubs);
break;
case "F":
PLM.displayStatistics(PLM.clubs);
break;
case "G":
GUInterface GUI = new GUInterface();
//PLM.displayTable(PLM.clubs);
break;
case "Q":
System.exit(0);
break;
default:
break;
}
} while(menuSelector != "q");
}

public class GUInterface {
JFrame frame;
JPanel jPanel = new JPanel();
Container c;
GUInterface()
{
frame = new JFrame("PremierLeageGUI");
JButton leagueTable = new JButton("Premier League Table");
JButton sortGoals = new JButton("Sort by goals scored, descending order");
JButton sortWins = new JButton("Sort by wins scored, descending order");
c = frame.getContentPane();
frame.setLayout(new FlowLayout());
c.add(leagueTable);
c.add(sortGoals);
c.add(sortWins);

leagueTable.addActionListener(new ActionEvents(frame, leagueTable));
frame.setSize(400,400);
frame.setVisible(true);
frame.getContentPane().setBackground(Color.pink);
frame.addWindowListener(new WindowListener());
}
}

public class ActionEvents implements ActionListener {
JFrame frame;
JButton showtable;
PremierLeagueManager p = new PremierLeagueManager();
ActionEvents(JFrame f, JButton b)
{
frame = f;
showtable = b;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == showtable)
{
p.displayTable(p.clubs);
}
}
}
//displayTable method inside PremierLeagueManager class
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).getWins();
Object[] row = {name, goals, points, wins};
model.addRow(row);
System.out.println(name);
}
final JTable teamTable = new JTable(model);
teamTable.setFillsViewportHeight(true);
JPanel panel = new JPanel();
JScrollPane scrollPane = new JScrollPane(teamTable);
panel.add(scrollPane);

JFrame frame = new JFrame("Tableview");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
teamTable.setOpaque(true);
frame.setContentPane(panel);
frame.pack();
frame.setSize(500, 500);
frame.setVisible(true);
}

您应该将现有的PremierLeagueManager传递到GUInterface构造函数中。

主要方法:

case "G":
GUInterface GUI = new GUInterface(PLM);
break;

GUInterface构造函数必须接受PremierLeagueManager作为参数。在GUI界面类中:

public class GUInterface {
// some lines left out
GUInterface(PremierLeagueManager plm)
{
// some lines left out
leagueTable.addActionListener(new ActionEvents(frame, leagueTable, plm));
// some lines left out
}

ActionEvent类的开头看起来是这样的:

public class ActionEvents implements ActionListener {
JFrame frame;
JButton showtable;
PremierLeagueManager p;
ActionEvents(JFrame f, JButton b, PremierLeagueManager plm)
{
frame = f;
showtable = b;
p = plm;
}
// some lines left out

最新更新