import java.io.*; // bring in all the stream i/o stuff import java.text.*; import javax.swing.*; class StudentList extends JTable{ /**************************************************************** * * this constructor creates the student list and fills it from * the specified file * @param filename - string with the name of the file */ StudentList(){ super(100,5); // this needs to go first, setup the vector } /**************************************************************** * * this constructor loads the student list from * the specified file * @param filename - string with the name of the file */ public void load(String filename) throws IOException { // // Create a file input stream (if possible) // try{ String line; FileReader file = new FileReader(filename); // create a file reader stream BufferedReader bReader = new BufferedReader(file); // convert it to a buffered reader so we can use readLine() boolean eof = false; int row = 0; while(!eof){ // while not at the end of the file line = bReader.readLine(); if(line == null) // end of file reached ?? eof= true; // then exit while loop else try{ Student s = new Student(line); setValueAt(s.lastName,row,0); setValueAt(s.firstName,row,1); setValueAt(s.major,row,2); setValueAt(s.creditsEarned,row,3); setValueAt(s.gradePointAverage,row,4); row++; } catch (ParseException e) { System.out.println("StudentList - bad record in file ["+filename+"]"); System.out.println(e.getMessage()); // skip record and go on to the next one. } } bReader.close(); // close the file } catch(IOException e){ System.out.println("IO Exception when loading studentlist from [" + filename + "] error: " + e.getMessage()); throw(e); } } /** * * Create a summary report in html format * */ void summaryHTMLReport(String filename) { int i; try{ HTMLPrinter file = new HTMLPrinter(filename, "Summary Student Report"); file.startCentering(); file.printHeading("Student List",2); file.paragraph(); file.startTable(1); file.startRow(); file.printBoldCell("Last Name"); file.printBoldCell("First Name"); file.printBoldCell("Major"); file.printBoldCell("Credits Earned"); file.printBoldCell("Grade Point Average"); file.endRow(); i=0; for(i=0; i < getRowCount(); i++){ if(getValueAt(i,0) == null) break; file.startRow(); file.printCell((String)getValueAt(i,0)); file.printCell((String)getValueAt(i,1)); file.printCell((String)getValueAt(i,2)); file.printCell((String)getValueAt(i,3)); file.printCell((String)getValueAt(i,4)); file.endRow(); } file.endTable(); file.stopCentering(); file.close(); } catch(IOException e) { JOptionPane.showMessageDialog(null,"Error writing to file : "+filename, "IOException",JOptionPane.ERROR_MESSAGE); // popup an error message } } } // end of StudentList class