在JTable中显示arrayLists的arrayList

时间:2013-02-07 14:26:10

标签: java swing arraylist jtable

我有一个arraylist,arrayList的每个元素都包含一个小的arrayList数据。

我需要根据这些数据创建一个表。你会建议什么是解决这个问题的最佳途径?

提前感谢您提供任何帮助

3 个答案:

答案 0 :(得分:3)

请参阅How to create a Table Model

一个小例子,展示如何创建模型并将对象存储在模型中。应使用该模型对数据进行任何操作。

创建模型并使用列表存储该表的数据。如下例所示。表中的每一行都是Student对象,每个对象都存储在列表中。遍历列表并获取每个对象并显示在表中。这是使用getValueAt(..)方法完成的。

JTable Model Example

import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;


public class JTableList {
    public static void main(String[] args) {

        Runnable r = new Runnable() {

            @Override
            public void run() {
                CustomModel model = new CustomModel();
                JTable table = new JTable();
                table.setModel(model);

                JFrame frame = new JFrame();
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);             
            }
        };

        EventQueue.invokeLater(r);
    }

}

class CustomModel extends AbstractTableModel {

    List<Student> data;
    String[] columnNames = {"Name", "Age"};

    public CustomModel() {
        data = new ArrayList<Student>();

        data.add(new Student("Amar", 1));
        data.add(new Student("Sam", 2));
        data.add(new Student("Amar", 1));
        data.add(new Student("Sam", 2));
        data.add(new Student("Amar", 1));
        data.add(new Student("Sam", 2));


    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public int getColumnCount() {
        return 2;
    }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Student student = data.get(rowIndex);
        switch (columnIndex) {
        case 0:
            return student.getName();
        case 1:
            return student.getAge();
        default:
            return null;
        }
    }

}

class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

答案 1 :(得分:1)

要显示列表列表的GUI表,我将创建一个实现“TableModel”接口的类,并且有一个构造函数可以接收“List&gt;”

public class ListTableModel <T> implements TableModel {

    private List<List<T>> source;

    public ListTableModel(List<List<T>> source) {

        this.source = source;

    }


    //Override 'getRowCount' 
    //The row count would be calculated as the size of the outer list.
    @Override
    public int getRowCount() {
        return source.size();
    }

    //Override 'getColumnCount'
    //The column count would be calculated as the max size of the inner lists
    @Override
    public int getColumnCount() {
        int max = 0;
        for(List<T> row : source) {
            max = Math.max(max, row.size());
        }
        return max;
    }

    //Override 'getColumnName'
    //Lets go ahead and just give a unique name to each column based on the index.
    //This could be populated from an input taken by the constructor, but we
    //won't worry about that now.
    @Override
    public String getColumnName(int columnIndex) {
        return "Column " + columnIndex;
    }

    //Override 'getColumnClass'
    //The class would technically be the generic type 'T', so to get this we 
    //will simply just get the calss of the first element.
    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return source.get(0).get(0).getClass();
    }


    //Override 'isCellEditable'
    //I'm going to assume we don't want cell to be editable.
    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        List<T> row = source.get(rowIndex);
        if(columnIndex >= row.size())
            return null;
        else 
            return row.get(columnIndex);
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        //required but we will assume that you cannot change the source list
        //if we needed to, it wouldn't be too difficult to implement.
    }

    @Override
    public void addTableModelListener(TableModelListener l) {
        //required but not used (will only be used if the source could change)
    }

    @Override
    public void removeTableModelListener(TableModelListener l) {
        //required but not used (will only be used if the source could change)
    }

}

您可以将此模型与您的列表一起使用:

List<List<String>> myList = new ArrayList();

//Populate 'myList'...

JTable table = new JTable();

//Add table to view...

table.setModel(new ListTableModel(myList));

编辑我可以告诉你如何实现TableModel。

答案 2 :(得分:1)

List Table Model可用于执行此操作。

这是一个更复杂,更灵活的解决方案,可以实现此线程中提供的早期建议。