将arrayList数据加载到JTable中

我正在尝试从名为FootballClub的方法中设置项目,到目前为止它还没问题。 但后来我从它创建了一个arrayList,我无法找到一种方法将这些信息存储到JTable中。 问题是我找不到设置固定行数的方法

这是我的代码:

类StartLeague:

 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; } 

SportsClub类(摘要):

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

最后,LeagueManager,这是一个接口:

 import java.util.ArrayList; public interface LeagueManager { ArrayList originalLeagueTable = new ArrayList(); 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); } 

有人能帮帮我吗? 我已经尝试并尝试了好几天。 谢谢。

“问题是我找不到设置固定行数的方法”

您不需要设置行数。 使用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[]tableModel添加行

 Object[] objs = {1, "Arsenal", 35, 11, 2, 2, 15, 30, 11, 19}; tableModel.addRow(objs); 

您可以循环添加Object []数组。

注意:JTable当前不允许将输入数据实例化为ArrayList 。 它必须是Vector或数组。

请参见JTable和DefaultTableModel 。 另外, 如何使用JTable教程

“我从中创建了一个arrayList,但我无法找到将这些信息存储到JTable中的方法。”

你可以做这样的事情来添加数据

 ArrayList originalLeagueList = new ArrayList(); 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); } 

您可能需要使用TableModel ( Oracle的教程 )

如何实现自己的TableModel

 public class FootballClubTableModel extends AbstractTableModel { private List clubs ; private String[] columns ; public FootBallClubTableModel(List aClubList){ super(); clubs = aClubList ; columns = new String[]{"Pos","Team","P", "W", "L", "D", "MP", "GF", "GA", "GD"}; } // Number of column of your table public int getColumnCount() { return columns.length ; } // Number of row of your table public int getRowsCount() { return clubs.size(); } // The object to render in a cell public Object getValueAt(int row, int col) { FootballClub club = clubs.get(row); switch(col) { case 0: return club.getPosition(); // to complete here... default: return null; } } // Optional, the name of your column public String getColumnName(int col) { return columns[col] ; } } 

您可能需要覆盖TableModel其他方法,取决于您想要做什么,但这里是理解和实现的基本方法:)
像这样使用它

 List clubs = getFootballClub(); TableModel model = new FootballClubTableModel(clubs); JTable table = new JTable(model); 

希望它有所帮助!

我从中创建了一个arrayList,但我无法找到将这些信息存储到JTable中的方法。

DefaultTableModel不支持显示存储在ArrayList中的自定义对象。 您需要创建自定义TableModel。

您可以查看Bean表模型 。 它是一个可重用的类,它将使用reflection来查找FootballClub类中的所有数据并显示在JTable中。

或者,您可以扩展上面链接中的Row Table Model ,通过实现一些方法,可以更轻松地创建自己的自定义TableModel。 JButtomTableModel.java源代码提供了如何执行此操作的完整示例。

您可以执行类似于我对List >的操作或任何其他Arraylist,从其他返回List的PingScan类返回的类型,因为它实现了服务执行程序。 无论如何,代码记下你可以使用foreach并从List中检索数据。

  PingScan p = new PingScan(); List> scanResult = p.checkThisIP(jFormattedTextField1.getText(), jFormattedTextField2.getText()); for (final Future f : scanResult) { try { if (f.get() instanceof String) { String ip = f.get(); Object[] data = {ip}; tableModel.addRow(data); } } catch (InterruptedException | ExecutionException ex) { Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex); } }