2013-12-11 55 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); 
        } 
       }