2013-12-11 53 views
12

我试图从名为FootballClub的方法中设置项目,到目前为止它没有问题。 但后来我从它创建了一个arrayList,我无法找到一种方法将这些信息存储到JTable中。 的问题是,我不能找到一种方法来设置行将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<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); 

} 

有人可以帮我吗? 我尝试了几天。 谢谢。

+0

我不认为JTable将ArrayList作为数据参数。您还需要一个Vector或二维数组 –

+0

,因为您创建新的足球俱乐部,但不会将它们添加到列表中,因此目前您的ArrayList将为空。 – DoubleDouble

回答

19

“问题是,我不能找到一种方法来设置行固定数量的”

你并不需要设置的行数。使用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); 

然后你就可以行到tableModelObject[]

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

tableModel.addRow(objs); 

您可以循环添加到您的添加对象[]数组。

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

参见JTableDefaultTableModel。此外,How to Use JTable tutorial

“我从它创建了一个ArrayList和我不知能不能找到一种方法来存储这些信息到一个JTable。“

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

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

} 
+0

所以这意味着我必须创建所有的get方法,对吧? – jPratas

+0

是的,为您的FootballClub –

+0

所以,它会是这样的: int max = Array.get(0); (array.get(i)> max){ max = array.get } } – jPratas

8

您可能需要使用TableModelOracle's tutorial here

如何实现自己的TableModel

public class FootballClubTableModel extends AbstractTableModel { 
    private List<FootballClub> clubs ; 
    private String[] columns ; 

    public FootBallClubTableModel(List<FootballClub> 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] ; 
    } 

} 

您可能需要重写0的其他方法,取决于你想要做什么,但这里是了解基本方法,并实现:)
使用方法如下

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

希望它能帮助!

+0

在你说完成的地方,这意味着我必须创建getPosition方法,对吧? – jPratas

+0

@jPratas在'getValueAt(int row,int col)'中,必须返回单元格中的元素。所以,如果col = 0,你返回一个足球俱乐部的位置,如果col = 1,你返回球队的名字,如果col = 2分,等等。这取决于足球的哪个属性你想要在列号“col”上显示的球杆。当然,你需要像getPosition()或getName()那样的getter ...也许你把属性设置为public来避免获取者,但这不是一个好的做法:) – NiziL

+0

对于这个任务,我不得不使用SportsClub作为抽象类。我在一个单独的项目中测试了你的方式,它工作得很好。非常感谢你的帮助:) – jPratas

2

我从它创建了一个arrayList,我以某种方式找不到将这些信息存储到JTable中的方法。

DefaultTableModel不支持显示存储在ArrayList中的自定义对象。您需要创建一个自定义TableModel。您可以查看Bean Table Model。这是一个可重用的类,它将使用反射来查找FootballClub类中的所有数据并显示在JTable中。

或者,您可以扩展在上述链接中找到的Row Table Model,以便通过实施一些方法更容易地创建自己的自定义TableModel。 JButtomTableModel.java源代码给出了一个完整的例子,说明如何做到这一点。

+0

+1 BeanTableModel,我不知道...真棒技巧! – NiziL

+0

对于这个任务,我不得不在java swing库中构建表。但是,这个答案非常有用,我将在未来的项目中使用这项技术。谢谢 :) – jPratas

0

你可以不喜欢什么,我与我的列表<未来从名为PingScan其他类返回列表>,因为它实现了服务执行返回<字符串>>或任何其他的ArrayList,类型做了。无论如何,代码下注意,您可以使用foreach并从列表检索数据。

PingScan p = new PingScan(); 
List<Future<String>> scanResult = p.checkThisIP(jFormattedTextField1.getText(), jFormattedTextField2.getText()); 
       for (final Future<String> 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); 
        } 
       }