简体   繁体   English

在JTable中显示arrayLists的arrayList

[英]Display an arrayList of arrayLists in a JTable

I have an arraylist and each element of the arrayList contains a small arrayList of data. 我有一个arraylist,arrayList的每个元素都包含一个小的arrayList数据。

I need to create a table from this data. 我需要根据这些数据创建一个表。 What would you recommend being the best route to take with solving this? 您建议什么是解决此问题的最佳途径?

Thanks in advance for any help 预先感谢您的任何帮助

See How to create a Table Model 请参见如何创建表模型

A small example showing how to create a Model and store your objects in the model. 一个小示例,展示了如何创建模型并将对象存储在模型中。 Any operation on the data should be done using the model. 对数据的任何操作都应使用模型来完成。

Create a model and use a list to store the data for that table. 创建一个模型并使用列表存储该表的数据。 As shown in the below example. 如下例所示。 Each of the row in the table is a Student object and each object is stored in the list. 表中的每一行都是一个Student对象,并且每个对象都存储在列表中。 Traverse the list and get each object and show in the table. 遍历列表并获取每个对象并在表中显示。 This is done using getValueAt(..) method. 这是使用getValueAt(..)方法完成的。

JTable模型示例

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

To display a GUI table of a List of lists, I would create a class that implements the 'TableModel' interface, and has a constructor that takes in your 'List>' 要显示列表列表的GUI表,我将创建一个实现“ TableModel”接口的类,并具有一个接受“ List>”的构造函数。

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

}

you would use this model with your list like so: 您将在列表中使用此模型,如下所示:

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

//Populate 'myList'...

JTable table = new JTable();

//Add table to view...

table.setModel(new ListTableModel(myList));

EDIT I can show you how to implement TableModel if you would like. 编辑我可以告诉你如何实现TableModel。

The List Table Model can be used to do this. 列表表模型可用于执行此操作。

It is a more complex and flexible solution implementing the earlier suggestions given in this thread. 这是一个更复杂和灵活的解决方案,实现了该线程中给出的早期建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM