有没有办法将ArrayList放入JTable,其中每一行是ArrayList的下一个索引?

我有许多不同的数组列表。 我希望每个索引都是JTable中的新行,但我不知道该怎么做。 我做了一个for循环,但它没有工作。 有没有办法用数组列表而不是数组填充JTable?

public TableCreator() { super(new GridLayout(1,0)); String[] columnNames = {"Item Type", "Description", "Size", "Price"}; // for(int i=0; i<ShoppingFunctions.cartType.size(); i++){ for(int i=0; i<GUI.size.size(); i++){//FIX!!!! item = ShoppingFunctions.cartType.get(i)+"\n"; described = GUI.describe[GUI.imageNum]; sizes = GUI.size.get(i); price = ShoppingFunctions.cartPrice.get(i)+"\n"; }//end of for Object[][] data = {{item, described, sizes, price}}; final JTable table = new JTable(data, columnNames); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true); if (DEBUG){ table.addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent e){ printDebugData(table); }//end of method });//end of listener }//end of if JScrollPane scrollPane = new JScrollPane(table); add(scrollPane); }//end of method 

简单的答案是基于AbstractTableModel类的东西创建自己的

例如…

 public abstract class AbstractGenericTableModel extends AbstractTableModel { protected ArrayList rows; protected List columnIdentifiers; public AbstractGenericTableModel() { this(0, 0); } public AbstractGenericTableModel(int rowCount, int columnCount) { this(new ArrayList(columnCount), rowCount); } public AbstractGenericTableModel(List columnNames, int rowCount) { setData(new ArrayList<>(rowCount), columnNames); } public AbstractGenericTableModel(List data, List columnNames) { setData(data, columnNames); } public List getRowData() { return rows; } private List nonNullVector(List v) { return (v != null) ? v : new ArrayList(); } public void setData(List data, List columnIdentifiers) { this.rows = new ArrayList<>(nonNullVector(data)); this.columnIdentifiers = nonNullVector(columnIdentifiers); fireTableStructureChanged(); } public void addRow(R rowData) { insertRow(getRowCount(), rowData); } public void insertRow(int row, R rowData) { rows.add(row, rowData); fireTableRowsInserted(row, row); } /** * Removes the row at row from the model. Notification of the row being removed will be sent to all the listeners. * * @param row the row index of the row to be removed * @exception ArrayIndexOutOfBoundsException if the row was invalid */ public void removeRow(int row) { rows.remove(row); fireTableRowsDeleted(row, row); } public void setColumnIdentifiers(List columnIdentifiers) { setData(rows, columnIdentifiers); } @Override public int getRowCount() { return rows.size(); } @Override public int getColumnCount() { return columnIdentifiers.size(); } @Override public String getColumnName(int column) { Object id = null; // This test is to cover the case when // getColumnCount has been subclassed by mistake ... if (column < columnIdentifiers.size() && (column >= 0)) { id = columnIdentifiers.get(column); } return (id == null) ? super.getColumnName(column) : id.toString(); } } public class ArrayListTableModel extends AbstractGenericTableModel { @Override public Object getValueAt(int rowIndex, int columnIndex) { List rows = getRowData(); return rows.get(rowIndex).get(columnIndex); } } 

这将创建两个类,一个基于“抽象generics”的模型,它允许您指定支持行的物理数据和允许您为单个数据使用ArrayList

但是,假设每行具有相同数量的元素,但不进行检查

我建议您仔细查看如何使用表格了解更多详情

您可以使用列表表模型 。 它将支持存储在ArrayListVector或任何其他实现List接口的类中的数据行。

ListTableModel还基于通用TableModel,它允许您将对象添加到模型中的行。 有许多基于行的方法可以轻松使用模型

ListTableModel提供了允许您轻松自定义模型的方法:

  1. setColumnClass – 指定各列的类,以便表可以使用正确的渲染器/编辑器。
  2. setModelEditable – 指定整个模型的可编辑属性。
  3. setColumnEditable – 在列级别指定可编辑属性。 此属性将优先于模型可编辑属性。

另一种选择是迭代ArrayList并将每行数据复制到Vector ,然后使用addRow(...)方法将Vector添加到DefaultTableModel