我有叫學生在這個對象,我有對象的ArrayList中包含的材料(姓名,註釋)如何以顯示包含一個又一個的ArrayList在JTable中有一個TableModel創建一個ArrayList的
Public Class Student{
private String StudentFirstName;
private String StudentLastName;
private List<Material> materials;
}
public Class Material{
String String materialName;
String Float note;
}
的對象我想在JTable中顯示所有數據,這樣的:
StudentFirstName:傑克StudentLastName:杜邦materialName:數學注:15 StudentFirst名稱:傑克StudentLastName:杜邦materialName:法語注意:12 StudentFirstName:傑克StudentLastName:杜邦materialName:數學注:15 StudentFirstName:傑克StudentLastName:杜邦materialName :體育筆記:10
學生名字:peter StudentLastName:桑切斯materialName:數學注:14 StudentFirstName:彼得StudentLastName:桑切斯materialName:法語注意:17 StudentFirstName:彼得StudentLastName:桑切斯materialName:阿拉伯語注: 11
這是我的表模型
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()); `
所以結果是:
StudentFirstName:傑克StudentLastName:杜邦materialName:數學注:15
StudentFirstName:彼得StudentLastName:sanche žmaterialName:數學注:14
thx爲您的答案我會測試你的第一個解決方案:) 第二我做到這一點之前,我問了這個問題我混合了兩個對象:D和它的工作,但我不明白,我想更好地模型化和按照我的班級示意圖@ user2896152 – user3299124
好的......讓我知道:) – user2896152