我有一个arraylist,arrayList 的每个元素都包含一个小的数据arrayList。
我需要根据这些数据创建一个表。你会推荐什么是解决这个问题的最佳途径?
提前感谢您的帮助
请参阅如何创建表模型
一个展示如何创建模型并将对象存储在模型中的小示例。对数据的任何操作都应该使用模型来完成。
创建一个模型并使用一个列表来存储该表的数据。如下例所示。表中的每一行都是一个Student
对象,每个对象都存储在列表中。遍历列表并获取每个对象并显示在表格中。这是使用getValueAt(..)
方法完成的。
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;
}
}
列表表模型可用于执行此操作。
这是一个更复杂、更灵活的解决方案,实现了这个线程中给出的早期建议。
要显示列表列表的 GUI 表,我将创建一个实现“TableModel”接口的类,并有一个接受“列表>”的构造函数
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。