一直在寻找,但没有找到我的ptoblem的解决方案,我正在尝试显示在课程中注册的学生的ArrayList,其中我使用JTable并通过JOptionPane显示它,我的问题是,我'无法显示学生的ArrayList,这是我的代码:
public static void showStudentsEnrolled(int indx) {
for(int x = 0; x<Course.get(indx).getEnrollees().size(); x++){
System.out.println(Course.get(indx).getEnrollees().get(x).toString()); //display enrolled students
}
ArrayList<Student> enrolled = Course.get(indx).getEnrollees();
StudentsTableModel model = new StudentsTableModel(enrolled);
JTable table = new JTable(model);
JScrollPane pane = new JScrollPane(table);
JPanel panel = new JPanel(new GridLayout());
panel.add(pane);
panel.setPreferredSize(new Dimension(600,400));
JOptionPane.showMessageDialog(null,panel, "Students Enrolled", JOptionPane.INFORMATION_MESSAGE);
}
你知道,我有一个自定义表模型,它接受一个ArrayList作为参数并将其用作数据, 我尝试使用我班上的另一个列表,如下所示:
ArrayList<Student> enrolled = Student; // Student is an ArrayList of Student
StudentsTableModel model = new StudentsTableModel(enrolled);
并成功填充表格中的数据
但是当我试图获得我的真正目标时:
ArrayList<Student> enrolled = Course.get(indx).getEnrollees();
StudentsTableModel model = new StudentsTableModel(enrolled);
出来的是一张空桌子,可能是什么造成了这个问题?
抱歉,我这里涉及很多课程,所以花了一些时间获得必要的代码,这是可运行的代码:
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import java.io.Serializable;
import java.util.*;
import java.awt.*;
public class Testy {
Student stud1 = new Student(2137019,1,"Havard","Jhon",'M',"BSCS");
Student stud2 = new Student(2137019,1,"Havard","Jhon",'M',"BSCS");
Student stud3 = new Student(2137019,1,"Havard","Jhon",'M',"BSCS");
Student stud4 = new Student(2137019,1,"Havard","Jhon",'M',"BSCS");
Student stud5 = new Student(2137019,1,"Havard","Jhon",'M',"BSCS");
Student stud6 = new Student(2137019,1,"Havard","Jhon",'M',"BSCS");
Student stud7 = new Student(2137019,1,"Havard","Jhon",'M',"BSCS");
Course course1 = new Course(9003,"ICS 112","Programming Fundamentals",2,"2:30","4:00","MTH","S521");
Course course2 = new Course(9003,"ICS 112","Programming Fundamentals",2,"2:30","4:00","MTH","S521");
Course course3 = new Course(9003,"ICS 112","Programming Fundamentals",2,"2:30","4:00","MTH","S521");
static ArrayList<Student> student = new ArrayList<Student>();
static ArrayList<Course> course = new ArrayList<Course>();
public Testy(){
student.add(stud1);
student.add(stud2);
student.add(stud3);
student.add(stud4);
student.add(stud5);
student.add(stud6);
student.add(stud7);
course.add(course1);
course.add(course2);
course.add(course3);
//Enroll a student
for(int x = 0; x < 5; x++){
course.get(0).enroll(student.get(x));}
// I want to show the enrolled students in course index 0;
showStudentsEnrolled(0);
}
public static void main (String args[]){
new Testy();
}
public static void showStudentsEnrolled(int indx) {
ArrayList<Student> enrolled = course.get(indx).getEnrollees();
for(int x = 0; x<enrolled.size(); x++){
System.out.println(enrolled.get(x).toString()); //display enrolled students
}
StudentsTableModel model = new StudentsTableModel(enrolled);
JTable table = new JTable(model);
JScrollPane pane = new JScrollPane(table);
JPanel panel = new JPanel(new GridLayout());
panel.add(pane);
panel.setPreferredSize(new Dimension(600,400));
JOptionPane.showMessageDialog(null,panel, "Students Enrolled", JOptionPane.INFORMATION_MESSAGE);
}
class Student {
private int id ;
private int yr ;
private String fname;
private String gname;
private char mname;
private String course;
private boolean isEnrolled ;
private ArrayList<Course> eCourse = new ArrayList<Course>();
public Student(int id,int yr, String f, String g, char m, String c){
this.id = id;
this.yr = yr;
fname = f;
gname = g;
mname = m;
course = c;
isEnrolled = false;
}
//GET THE ID NUMBER OF A STUDENT
public int getId(){
return id;
}
//GET THE YEAR LEVEL OF A STUDENT
public int getYr(){
return yr;
}
//GET THE FAMILY NAME OF A STUDENT
public String getFname(){
return fname;
}
//GET THE GIVEN NAME OF A STUDENT
public String getGname(){
return gname;
}
//GET THE MIDDILE INITIAL OF A STUDENT
public char getMname(){
return mname;
}
//GET THE COURSE OF A STUDENT
public String getCourse(){
return course;
}
//SET ID NUMBER OF A STUDENT
public void setId(int ids){
id = ids;
}
//SET YEAR LEVEL OF A STUDENT
public void setYr(int yrs){
yr = yrs;
}
//SET THE FAMILY NAME OF A STUDENT
public void setFname(String fnames){
fname = fnames;
}
//SET THE GIVEN NAME OF A STUDENT
public void setGname(String gnames){
gname = gnames;
}
//SET THE MIDDILE INITIAL OF A STUDENT
public void setMname(char mnames){
mname = mnames;
}
//SET THE COURSE OF A STUDENT
public void setCourse(String courses){
course = courses;
}
}
class Course implements Serializable {
private String startTime;
private String endTime;
private int units;
private int courseCode;
private String courseNumber;
private String courseDescription;
private String room;
private String days;
private String status;
private String schedule;
private boolean open ;
private ArrayList <Student> enrollees = new ArrayList<Student>();
/**CONSTRUCTOR FOR THE COURSE/*
* accepts values to be passed on Course
* @param classCode int that will serve as the class code of the class
* @param courseNum string subject of the course
* @param courseDes string descriptive title of the course
* @param unit int units of the course
* @param time1 String time in which the course begins
* @param time2 String time in which the course dismiss
* @param day String days abbreviation in which the course takes place
* @param rooms String room in which the course takes place
*/
public Course(int classCode,String courseNum,String courseDes,int unit,String time1,String time2,String day,String rooms){
courseCode = classCode;
courseNumber = courseNum;
courseDescription = courseDes;
units = unit;
startTime =time1;
endTime = time2;
room = rooms;
days = day;
schedule = startTime + "-" + endTime + " " + days;
open = true;
status = "Open";
}
public void enroll(Student student) {
enrollees.add(student);
}
//get the students enrolled in the course
public ArrayList <Student> getEnrollees(){
return enrollees;
}
}
public static class StudentsTableModel extends AbstractTableModel {
boolean DEBUG = false;
private String[] columnNames = {"ID Number","Last Name","First Name","Middle Initial","Course","Year"};
private static ArrayList<Student> data ;
public StudentsTableModel(ArrayList<Student> students){
data = new ArrayList<Student>();
for(int x = 0; x<students.size(); x++){
//checks if the student is available for enrollment
if(students.get(x).isEnrolled() == false){
data.add(students.get(x));}
else if(students.get(x).isEnrolled() == true){// Do nothing , don't add to the list
}
}
// save changes
fireTableDataChanged();
}
public static void setArrayData(ArrayList<Student> student){
data = student;
}
public static ArrayList<Student> getCurrentArray(){
return data;
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.size();
}
public String getColumnName(int col) {
return columnNames[col];
}
// Customize getValueAt method
public Object getValueAt(int row, int col) {
Object object = null;
switch (col){
case 0 : object = data.get(row).getId(); break;
case 1 : object = data.get(row).getFname(); break;
case 2 : object = data.get(row).getGname(); break;
case 3 : object = data.get(row).getMname(); break;
case 4 : object = data.get(row).getCourse(); break;
case 5 : object = data.get(row).getYr(); break;
}
return object;
}
public Class<? extends Object> getColumnClass(int c) {
Object a = 1;
Object b = "String";
Object e = 'd';
if(c == 0 || c==5)
return a.getClass();
else if(c==1 || c==2 || c==4)
return b.getClass();
else if(c==3)
return e.getClass();
else{ return getValueAt(0,c).getClass(); }
}
// Make all the fields constant
public boolean isCellEditable(int row, int col) {
if (col < 5) {
return false;
} else {
return true;
}
}
// Customize setValueAt method
public void setValueAt(Object value, int row, int col) {
if (DEBUG) {
System.out.println("Setting value at " + row + "," + col
+ " to " + value
+ " (an instance of "
+ value.getClass() + ")");
}
switch (col){
case 0: data.get(row).setId((Integer)value); break;
case 1: data.get(row).setFname((String)value); break;
case 2: data.get(row).setGname((String)value); break;
case 3: data.get(row).setMname((Character)value); break;
case 4: data.get(row).setCourse((String)value); break;
case 5: data.get(row).setYr((Integer)value); break;
}
fireTableCellUpdated(row, col);
}
}
}
还是一张空桌子; 有人可以帮我吗?这可能是一个简单的问题,但我很难弄清楚原因,提前谢谢