Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用tableModel显示Jtable中包含另一个ArrayList的ArrayList_Java_Swing_Arraylist_Abstracttablemodel - Fatal编程技术网

Java 如何使用tableModel显示Jtable中包含另一个ArrayList的ArrayList

Java 如何使用tableModel显示Jtable中包含另一个ArrayList的ArrayList,java,swing,arraylist,abstracttablemodel,Java,Swing,Arraylist,Abstracttablemodel,我有一个名为Student的对象,在这个对象中我有一个包含材质(名称、注释)的对象的arrayList 因此,结果是: 学生名:杰克学生名:杜邦材料名:数学注:15 学生名:彼得学生名:桑切斯材料名:数学笔记:14我会这样做: 首先,方法getRowCount() 实际上这个值是2,所以您告诉您的DataTableModel,您的表包含2行,这是不正确的。 尝试通过以下操作修改此方法: @Override public int getRowCount() { int row_count=0;

我有一个名为Student的对象,在这个对象中我有一个包含材质(名称、注释)的对象的arrayList

因此,结果是:

学生名:杰克学生名:杜邦材料名:数学注:15


学生名:彼得学生名:桑切斯材料名:数学笔记:14

我会这样做:

首先,方法getRowCount()

实际上这个值是2,所以您告诉您的DataTableModel,您的表包含2行,这是不正确的。 尝试通过以下操作修改此方法:

@Override
public int getRowCount() {
  int row_count=0;
  for (int i=0; i<allStudents.size(); i++) {
    row_cont+=allStudents.get(i).getMaterials().size();
} 

希望这能给你一个有用的提示。嗨

thx对于你的回答,我将测试你的第一个解决方案:)第二个,在我提问之前,我混合了两个对象:D,它工作了,但我不欣赏,我想更好地建模并遵循我的类图@user2896152
package com.orange.tableModel;
public class DataTableModel extends AbstractTableModel {
String[] entete = {"StudentFisrtName", "StudentLastNameName", 
"Mat    erialName", "MaterialNote"};
List<Student> allStudents;
public DataTableModel() {
allStudents = new ArrayList<>();

}

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

}

@Override
public int getColumnCount() {
    return entete.length;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {

    switch (columnIndex) {
        case 0: {
           return allStudents.get(rowIndex).getStudentFirstName();

        }
        case 1: {
            return allStudents.get(rowIndex).getStudentLastName();

        }
        case 2: {

            return allStudents.get(rowIndex).materials(rowIndex).getMaterialName();
        }
        case 3: {
            return allStudents.get(rowIndex).materials(rowIndex).getNote();
        }

        default:
            throw new IllegalArgumentException();
    }

}

@Override
public String getColumnName(int column) {

    return entete[column];
}
Jtable dataTable=new Jtable();
dataTable.setModel(new DataTableModel());     `
returns allStudents.size()
@Override
public int getRowCount() {
  int row_count=0;
  for (int i=0; i<allStudents.size(); i++) {
    row_cont+=allStudents.get(i).getMaterials().size();
} 
if (count_row< allStudents.get(rowIndex).getMaterials().size()) {
  //show information of the same Student but with materials(count_row)
}
Student A_B=new Student("A","B","Math",12);    //first_name, last_name, subject, note
Student A_B=new Student("A","B","History",4);
Student B_C=new Student("B","C","Geograph",10);