从结果列表构建Java TableModel

时间:2013-03-24 22:19:55

标签: java swing jtable tablemodel defaulttablemodel

嗨我在填充TableModel时遇到问题,我无法理解问题是什么

这是我的方法

 private TableModel buildTableModel(List<Player> result) {
        // build the columns
        Vector<String> columnNames = new Vector<String>();
        //int columnCount = metaData.getColumnCount();
        //for (int column = 1; column <= columnCount; column++) {
        //    columnNames.add(metaData.getColumnName(column));
        //}

        columnNames.add("playerid");
        columnNames.add("squeezePlay");
        columnNames.add("weakShowdown");
        columnNames.add("numberOfPlays");
        columnNames.add("playsWithFriends");
        columnNames.add("suspend");
        columnNames.add("grade");


        // data of the table
        Vector<Vector<Object>> data = new Vector<Vector<Object>>();
        while (result.iterator().hasNext()) {
            Player player = result.iterator().next();

            Vector<Object> vector = new Vector<Object>();

             vector.add((Object) player.GetId());
             vector.add((Object) player.GetSqueezePlay());
             vector.add((Object) player.GetWeakShowdown());
             vector.add((Object) player.GetNumberOfPlays());
             vector.add((Object) player.GetPlaysWithFriends());
             vector.add((Object) player.GetSuspended());
             vector.add((Object) player.GetGrade());

             data.add(vector);
        }

        return new DefaultTableModel(data, columnNames);

    }

注意:无论有没有Object投射,该表仍无法正常工作..

请建议填充TableModel的任何替代解决方案。

谢谢!

1 个答案:

答案 0 :(得分:4)

每次拨打result.iterator()时,您都在阅读列表的开头。相反,使用这个:

for (Player player : result)