Load arrayList data into JTable

后端 未结 4 503
无人及你
无人及你 2020-11-29 05:16

I\'m trying to set items from a method called FootballClub and so far it\'s fine. but then I created an arrayList from it and I somehow can\'t find a way to sto

相关标签:
4条回答
  • 2020-11-29 05:57

    You can do something like what i did with my List< Future< String > > or any other Arraylist, Type returned from other class called PingScan that returns List> because it implements service executor. Anyway the code down note that you can use foreach and retrieve data from the List.

     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);
                        }
                    }
    
    0 讨论(0)
  • 2020-11-29 05:58

    "The problem is that i cant find a way to set a fixed number of rows"

    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

    "I created an arrayList from it and I somehow can't find a way to store this information into a JTable."

    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);
    
    }
    
    0 讨论(0)
  • 2020-11-29 06:12

    I created an arrayList from it and I somehow can't find a way to store this information into a JTable.

    The DefaultTableModel doesn't support displaying custom Objects stored in an ArrayList. You need to create a custom TableModel.

    You can check out the Bean Table Model. It is a reusable class that will use reflection to find all the data in your FootballClub class and display in a JTable.

    Or, you can extend the Row Table Model found in the above link to make is easier to create your own custom TableModel by implementing a few methods. The JButtomTableModel.java source code give a complete example of how you can do this.

    0 讨论(0)
  • 2020-11-29 06:14

    You probably need to use a TableModel (Oracle's tutorial here)

    How implements your own 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] ;
      }
    
    }
    

    You maybe need to override anothers methods of TableModel, depends on what you want to do, but here is the essential methods to understand and implements :)
    Use it like this

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

    Hope it help !

    0 讨论(0)
提交回复
热议问题