当前位置: 首页 > 知识库问答 >
问题:

将arrayList数据加载到JTable中

长孙星汉
2023-03-14

我试图从一个名为footballclub的方法中设置项目,到目前为止还不错。但后来我从它创建了一个arrayList,不知何故,我找不到将这些信息存储到JTable中的方法。问题是我找不到设置固定行数的方法

下面是我的代码:

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

public class startLeague implements LeagueManager{

//setting the frame and other components to appear

public startLeague(){
    JButton createTeam = new JButton("Create Team");
    JButton deleteTeam = new JButton("Delete Team");

    JFrame frame = new JFrame("Premier League System");
    JPanel panel = new JPanel();
    frame.setSize(1280, 800);
    frame.setVisible(true);
    frame.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    String col[] = {"Pos","Team","P", "W", "L", "D", "MP", "GF", "GA", "GD"};

    panel.setLayout(new GridLayout(20, 20));
    panel.add(createTeam);
    panel.add(deleteTeam);
    panel.add(new JLabel(""));
    //JLabels to fill the space
    }
    }
import java.util.ArrayList;



 public class FootballClub extends SportsClub{





   FootballClub(int position, String name, int points, int wins, int defeats, int draws, int totalMatches, int goalF, int goalA, int goalD){
   this.position = position;
   this.name = name;
   this.points = points;
   this.wins = wins;
   this.defeats = defeats;
   this.draws = draws;
   this.totalMatches = totalMatches;
   this.goalF = goalF;
   this.goalA = goalA;
   this.goalD = goalD;

   }
abstract class SportsClub {
int position;
String name;
int points;
int wins;
int defeats;
int draws;
int totalMatches;
int goalF;
int goalA;
int goalD;

}
import java.util.ArrayList;


public interface LeagueManager {
ArrayList<FootballClub> originalLeagueTable = new ArrayList<FootballClub>();
FootballClub arsenal = new FootballClub(1, "Arsenal", 35, 11, 2, 2, 15, 30, 11, 19);
FootballClub liverpool = new FootballClub(2, "Liverpool", 30, 9, 3, 3, 15, 34, 18, 16);
FootballClub chelsea = new FootballClub(3, "Chelsea", 30, 9, 2, 2, 15, 30, 11, 19);
FootballClub mCity = new FootballClub(4, "Man City", 29, 9, 2, 4, 15, 41, 15, 26);
FootballClub everton = new FootballClub(5, "Everton", 28, 7, 1, 7, 15, 23, 14, 9);
FootballClub tot = new FootballClub(6, "Tottenham", 27, 8, 4, 3, 15, 15, 16, -1);
FootballClub newcastle = new FootballClub(7, "Newcastle", 26, 8, 5, 2, 15, 20, 21, -1);
FootballClub south = new FootballClub(8, "Southampton", 23, 6, 4, 5, 15, 19, 14, 5);

}

共有1个答案

祁宾白
2023-03-14

问题是我找不到固定行数的方法

您不需要设置行数。使用tablemodel。特别是DefaultTableModel

String col[] = {"Pos","Team","P", "W", "L", "D", "MP", "GF", "GA", "GD"};

DefaultTableModel tableModel = new DefaultTableModel(col, 0);
                                            // The 0 argument is number rows.

JTable table = new JTable(tableModel);

然后,可以使用对象[]表模型添加行

Object[] objs = {1, "Arsenal", 35, 11, 2, 2, 15, 30, 11, 19};

tableModel.addRow(objs);

您可以执行以下操作来添加数据

ArrayList<FootballClub> originalLeagueList = new ArrayList<FootballClub>();

originalLeagueList.add(new FootballClub(1, "Arsenal", 35, 11, 2, 2, 15, 30, 11, 19));
originalLeagueList.add(new FootballClub(2, "Liverpool", 30, 9, 3, 3, 15, 34, 18, 16));
originalLeagueList.add(new FootballClub(3, "Chelsea", 30, 9, 2, 2, 15, 30, 11, 19));
originalLeagueList.add(new FootballClub(4, "Man City", 29, 9, 2, 4, 15, 41, 15, 26));
originalLeagueList.add(new FootballClub(5, "Everton", 28, 7, 1, 7, 15, 23, 14, 9));
originalLeagueList.add(new FootballClub(6, "Tottenham", 27, 8, 4, 3, 15, 15, 16, -1));
originalLeagueList.add(new FootballClub(7, "Newcastle", 26, 8, 5, 2, 15, 20, 21, -1));
originalLeagueList.add(new FootballClub(8, "Southampton", 23, 6, 4, 5, 15, 19, 14, 5));

for (int i = 0; i < originalLeagueList.size(); i++){
   int position = originalLeagueList.get(i).getPosition();
   String name = originalLeagueList.get(i).getName();
   int points = originalLeagueList.get(i).getPoinst();
   int wins = originalLeagueList.get(i).getWins();
   int defeats = originalLeagueList.get(i).getDefeats();
   int draws = originalLeagueList.get(i).getDraws();
   int totalMatches = originalLeagueList.get(i).getTotalMathces();
   int goalF = originalLeagueList.get(i).getGoalF();
   int goalA = originalLeagueList.get(i).getGoalA();
   in ttgoalD = originalLeagueList.get(i).getTtgoalD();

   Object[] data = {position, name, points, wins, defeats, draws, 
                               totalMatches, goalF, goalA, ttgoalD};

   tableModel.add(data);

}
 类似资料:
  • 问题内容: 我正在尝试通过一种称为的方法设置项目,到目前为止还可以。但是后来我从中创建了一个arrayList,但我不知何故找不到将这些信息存储到JTable中的方法。问题是我找不到设置固定行数的方法 这是我的代码: 上课开始联赛: 足球俱乐部课程: SportsClub课程(摘要): 最后是LeagueManager,它是一个接口: 有人能帮帮我吗?我已经尝试了好几天。谢谢。 问题答案: “问题

  • 我已经创建了一个XML解析器来将XML文件中的信息检索到java,然后我尝试将这些数据存储到ArrayList中,以便将ArrayList用于我的方法。当我打印出来时,它似乎工作得很好。然而,由于某种原因,我得到了一个名为getAllRoutes的方法,它返回了错误的路由数

  • 问题内容: 我知道如何从Java数据库中检索数据(只是表中的文本),以及如何在控制台中显示数据。但我想将其加载到JTable中。有没有使用Vector的良好(现代)方法(教程)? 问题答案: 查看本教程:如何使用表

  • 问题内容: 我有一个 对象,我需要在JTable中显示此数组的内容。 Map-键是列名,对象是数据,我该如何放置? Baiscally Map包含一个表中的多行,并且所有行都添加到了数组列表中,现在我需要以表的形式在Swing应用程序中显示它们,并对它们执行排序和过滤。 问题答案: 对于数据,将您的列表嵌入到TableModel中。一的DefaultTableModel是最好的。 对于列,将您的列

  • 我有一个ArrayString,其中包含一些我想要打印在GUI上的数据,我开始使用JPanel之类的,最后使用Eclipse上的windowbuilder来帮助我修改可视化方面,我认为我的代码很糟糕(或者至少看起来很糟糕)我发现了一些与我的问题类似的线程,但我并不真正理解: 我可以不使用Jtable来代替看起来像一堆废话的东西吗?我只是无法理解JTable,我希望它看起来像我的代码输出,但我不明白

  • 我意识到这个标题似乎很混乱,所以我试着举一个例子。 我有一个JTable,我希望它填充一个名为Rapport的自定义对象的ArrayList。 我知道使用cellEditor制作特定的颜色来包含JCombobox,但我不知道如何按照描述从ArrayList加载数据。 有什么建议吗?