本文介绍了装载ArrayList的数据转换成的JTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从一个名为方法设置的项目 FootballClub
,到目前为止,它的罚款。
但后来我从它创建了一个ArrayList和我有点不能找到一种方法来存储这些信息到一个JTable。
问题是,我不能找到一种方法来设置行的固定数量
下面是我的code:
类StartLeague:
进口的javax.swing *。
导入的javax.swing.table *。
进口java.awt中的*。公共类startLeague实现LeagueManager {//设置框架和其他组件出现公共startLeague(){
JButton的createTeam =的新的JButton(创建组);
JButton的deleteTeam =的新的JButton(删除小组); JFrame的帧=新的JFrame(premier联赛系统);
的JPanel面板=新JPanel();
frame.setSize(1280,800);
frame.setVisible(真);
frame.add(面板);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
串COL [] = {正,队,P,W,L,D,MP,GF,GA,GD}; panel.setLayout(新的GridLayout(20,20));
panel.add(createTeam);
panel.add(deleteTeam);
panel.add(新的JLabel());
//的JLabel以填补空间
}
}
足球俱乐部类别:
进口的java.util.ArrayList; 公共类FootballClub扩展体育俱乐部{ FootballClub(INT位置,字符串名称,诠释点,诠释获胜,诠释连败,INT平局,INT totalMatches,诠释goalF,诠释goalA,诠释概略性的东西){
this.position =位置;
this.name =名称;
this.points =分;
this.wins =胜;
this.defeats =失败;
this.draws =绘制;
this.totalMatches = totalMatches;
this.goalF = goalF;
this.goalA = goalA;
this.goalD =概略性的东西; }
在体育俱乐部类(abstract):
抽象类体育俱乐部{
INT位置;
字符串名称;
INT点;
INT胜;
诠释失败;
INT绘制;
INT totalMatches;
INT goalF;
诠释goalA;
INT概略性的东西;}
最后,LeagueManager,这是一个接口
进口的java.util.ArrayList;
公共接口LeagueManager {
ArrayList的< FootballClub> originalLeagueTable =新的ArrayList< FootballClub>();
FootballClub阿森纳=新FootballClub(1,阿森纳,35,11,2,2,15,30,11,19);
FootballClub利物浦=新FootballClub(2,利物浦,30,9,3,3,15,34,18,16);
FootballClub切尔西=新FootballClub(3,切尔西,30,9,2,2,15,30,11,19);
FootballClub mCity =新FootballClub(4,人之城,29日,9,2,4,15,41,15,26);
FootballClub埃弗顿=新FootballClub(5,埃弗顿,28,7,1,7,15,23,14,9);
FootballClub TOT =新FootballClub(6,热刺,27,8,4,3,15,15,16,-1);
FootballClub纽卡斯尔=新FootballClub(7,纽卡斯尔,26日,8,5,2,15,20,21,-1);
FootballClub南=新FootballClub(8南安普敦,23,6,4,5,15,19,14,5);}
有人可以帮帮我吗?
我想,努力了好几天。
谢谢你。
解决方案
You don't need to set the number of rows. Use a TableModel
. A DefaultTableModel
in particular.
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);
Then you can add rows to the tableModel
with an Object[]
Object[] objs = {1, "Arsenal", 35, 11, 2, 2, 15, 30, 11, 19};
tableModel.addRow(objs);
You can loop to add your Object[] arrays.
Note: JTable does not currently allow instantiation with the input data as an ArrayList
. It must be a Vector
or an array.
See JTable and DefaultTableModel. Also, How to Use JTable tutorial
You can do something like this to add the data
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!