package a; import java.io.*; import java.awt.*; import javax.swing.*; import jav
ID: 3650497 • Letter: P
Question
package a;import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class StudentContainer extends JFrame implements ActionListener {
public int counter = 0;
private static final long serialVersionUID = 1L;
final int num_students = 50;
Student students[] = new Student[num_students];
String line[] = new String [50];
JLabel lblFirstName = new JLabel(" First Name:"); //creates lbl
JLabel lblLastName = new JLabel(" Last Name:"); //creates lbl
JLabel lblAddressLine1 = new JLabel(" Address Line 1:"); //creates lbl
JLabel lblAddressLine2 = new JLabel(" Address Line 2:"); //creates lbl
JLabel lblCity = new JLabel(" City:"); //creates lbl
JLabel lblState = new JLabel(" State:"); //creates lbl
JLabel lblZip = new JLabel(" Zip Code:"); //creates lbl
JLabel lblBirthday = new JLabel(" Date of Birth:"); //creates lbl
JLabel lblGradDate = new JLabel(" Graduation Date:"); //creates lbl
JLabel lblGpa = new JLabel(" GPA:"); //creates lbl
JLabel lblCreditHours = new JLabel(" Credit Hours:"); //creates lbl
JButton clickSave = new JButton("Save"); //creates Save button
JButton clickPrevious = new JButton("Previous"); //creates Previous button
JButton clickNext = new JButton("Next"); //creates Next button
JButton clickDelete = new JButton("Delete"); //creates Delete button
JTextField FirstNameInput = new JTextField(""); //creates NameInput text field
JTextField LastNameInput = new JTextField("");
JTextField AddressLine1Input = new JTextField("");
JTextField AddressLine2Input = new JTextField("");
JTextField CityInput = new JTextField("");
JTextField StateInput = new JTextField("");
JTextField ZipInput = new JTextField("");
JTextField BirthdayInput = new JTextField("");
JTextField GradDateInput = new JTextField("");
JTextField GpaInput = new JTextField("");
JTextField CreditHoursInput = new JTextField("");
public StudentContainer() {
super("STUDENT DATABASE"); //container header is "STUDENT DATABASE"
newGUI(); //calls newGUI to initialize GUI
readFile();
updateScreen();
} // end constructor{
private void newGUI(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //exit on close statement
Container p = this.getContentPane(); //declare container p
p.setLayout(new BorderLayout()); //set p to BorderLayout
Panel pCenter = new Panel(); //creates pCenter panel
pCenter.setLayout(new GridLayout(0,2)); //initiates grid layout
pCenter.add(lblFirstName); //adds Name label to pCenter
pCenter.add(FirstNameInput); //adds Phone text field to pCenter
pCenter.add(lblLastName); //adds Name label to pCenter
pCenter.add(LastNameInput); //adds Phone text field to pCenter
pCenter.add(lblAddressLine1); //adds Name text field to pCenter
pCenter.add(AddressLine1Input); //adds Phone text field to pCenter
pCenter.add(lblAddressLine2); //adds Name label to pCenter
pCenter.add(AddressLine2Input); //adds Phone text field to pCenter
pCenter.add(lblCity); //adds Name label to pCenter
pCenter.add(CityInput); //adds Phone text field to pCenter
pCenter.add(lblState); //adds Name label to pCenter
pCenter.add(StateInput); //adds Phone text field to pCenter
pCenter.add(lblZip); //adds Name label to pCenter
pCenter.add(ZipInput); //adds Phone text field to pCenter
pCenter.add(lblBirthday); //adds Name label to pCenter
pCenter.add(BirthdayInput); //adds Phone text field to pCenter
pCenter.add(lblGradDate); //adds Name label to pCenter
pCenter.add(GradDateInput); //adds Phone text field to pCenter
pCenter.add(lblGpa); //adds Name label to pCenter
pCenter.add(GpaInput); //adds Phone text field to pCenter
pCenter.add(lblCreditHours); //adds Name label to pCenter
pCenter.add(CreditHoursInput); //adds Phone text field to pCenter
Panel pSouth = new Panel(); //creates pSouth panel
pSouth.add(clickPrevious); //adds previous button
pSouth.add(clickSave); //adds save button
pSouth.add(clickDelete); //adds delete button
pSouth.add(clickNext); //adds next button
p.add(pCenter, BorderLayout.CENTER); //adds pCenter panel to container
p.add(pSouth, BorderLayout.SOUTH); //adds pSouth panel to container
clickPrevious.addActionListener(this); //assigns ActionListener to previous button
this.pack();
this.setVisible(true);
clickSave.addActionListener(this); //assigns ActionListener to save button
this.pack();
this.setVisible(true);
clickDelete.addActionListener(this); //assigns ActionListener to delete button
this.pack();
this.setVisible(true);
clickNext.addActionListener(this); //assigns ActionListener to next button
this.pack();
}//end newGUI
public void actionPerformed(ActionEvent e) {
// if (e.getSource() == clickSave){ //if save button is clicked, save unless there is nothing to save
// String x = NameInput.getText();
// String y = AddressInput.getText();
// String z = PhoneInput.getText();
//
// if(x.equals("")){ //if name is blank
// JOptionPane.showMessageDialog(null, "Name must be entered!"); //display error message
// }//end else if
// else if(y.equals("")){ //if address is blank
// JOptionPane.showMessageDialog(null, "Address must be entered!"); //display error message
// }//end else if
// else if(z.equals("")){ //if phone number is blank
// JOptionPane.showMessageDialog(null, "Phone Number must be entered!"); //display error message
// }//end else if
// else{ //if all items are entered
// Name.add(NameInput.getText()); //updates list with Name
// Address.add(AddressInput.getText()); //updates list with Address
// Phone.add(PhoneInput.getText()); //updates list with Phone
// WriteFile(); //calls WriteFile to save to the text file
// counter = Name.size() - 1; //takes counter to last record of list
// JOptionPane.showMessageDialog(null, "Save is complete!"); //display message to confirm save to user
// }//end else
//
// }//end if
//
// else if (e.getSource() == clickDelete){ //if delete is clicked, delete unless there is nothing to delete
// if(counter >= 0){ //if the counter is greater than 0
// Name.remove(counter); //remove name from name list
// Address.remove(counter); //remove address from address list
// Phone.remove(counter); //remove phone number from phone list
// WriteFile(); //deletes record from text file
// if (counter == Name.size()){ //if counter was last record
// if (Name.size() == 0) //if size is empty then counter becomes -1
// counter = -1;
// else
// counter = counter - 1; //sets counter to previous record
// }
// updateScreen(); //call update method to display blank text fields
// JOptionPane.showMessageDialog(null, "Entry has been Deleted!"); //display message to confirm save to user
// }//end if
// else {
// BlankGUI(); //if counter is not greater than 0
// JOptionPane.showMessageDialog(null, "There are no records!"); //display error message to user
// }//end else
// }//end else if
//
// else if (e.getSource() == clickPrevious){ //if previous is clicked, go to previous unless there is no previous
// if(counter > 0){ //if counter is greater than 0
// counter--;
// updateScreen(); //call update method to display blank form
// }
// else
// JOptionPane.showMessageDialog(null, "This is the first record"); //display message to user
// }//end else if
//
// else if (e.getSource() == clickNext){ //if next is clicked, go to next unless there is nothing next.
if (e.getSource() == clickNext){
FirstNameInput.setText(students[counter].firstName);
}
// if(counter > -1){ //if the arrayList is not empty
//
// if (counter<Name.size() - 1){ //if we are not at the end of the ArrayList, go to next element
// counter++; //go to next record
// updateScreen();
// }//end if
// else JOptionPane.showMessageDialog(null, "This is the last record!"); //else display message to let user know we
are at the end
//
// }//end if
// else JOptionPane.showMessageDialog(null, "There are no records!"); //display message to let user know there are
no records
//
// }//end else if
//
// else JOptionPane.showMessageDialog(null, "Error!");
}
public void readFile(){
for(int j = 0; j<50; j++){
students[j] = new Student();
}
BufferedReader readBuffer;
try {
readBuffer = new BufferedReader(new FileReader("Students.txt"));
String strRead;
int p = 0;
while((strRead = readBuffer.readLine())!=null){
//System.out.println("readFile first step in while");
String line1 = (String) strRead;
// System.out.println(line1);
// System.out.println(line1);
// System.out.println("readFile first step in while2");
students[p].parse(line1);
// System.out.println("readFile after first parse call");
// System.out.println(line[p]);
p++;
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void updateScreen() { //Updates text fields with requested data
// //updates screen with info
// if(counter == -1){
// // BlankGUI();
// }
//
// else {FirstNameInput.setText((String) students[counter].firstName); //uses counter for ArrayList
//
FirstNameInput.setText(students[counter].firstName);
LastNameInput.setText(students[counter].lastName);
AddressLine1Input.setText(students[counter].addressLine1);
AddressLine2Input.setText(students[counter].addressLine2);
CityInput.setText(students[counter].city);
StateInput.setText(students[counter].state);
ZipInput.setText(students[counter].zip);
//BirthdayInput.setText(students[counter].birthday.month); //birthday cannot be resolved?
//GradDateInput.setText(students[counter].graduation.month);
GpaInput.setText(students[counter].gpa);
CreditHoursInput.setText(students[counter].creditHours);
// AddressInput.setText((String) Address.get(counter));
// PhoneInput.setText((String) Phone.get(counter));
}
}
Explanation / Answer
import java.io.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class StudentContainer extends JFrame implements ActionListener { public int counter = 0; private static final long serialVersionUID = 1L; final int num_students = 50; Student students[] = new Student[num_students]; String line[] = new String [50]; JLabel lblFirstName = new JLabel(" First Name:"); //creates lbl JLabel lblLastName = new JLabel(" Last Name:"); //creates lbl JLabel lblAddressLine1 = new JLabel(" Address Line 1:"); //creates lbl JLabel lblAddressLine2 = new JLabel(" Address Line 2:"); //creates lbl JLabel lblCity = new JLabel(" City:"); //creates lbl JLabel lblState = new JLabel(" State:"); //creates lbl JLabel lblZip = new JLabel(" Zip Code:"); //creates lbl JLabel lblBirthday = new JLabel(" Date of Birth:"); //creates lbl JLabel lblGradDate = new JLabel(" Graduation Date:"); //creates lbl JLabel lblGpa = new JLabel(" GPA:"); //creates lbl JLabel lblCreditHours = new JLabel(" Credit Hours:"); //creates lbl JButton clickSave = new JButton("Save"); //creates Save button JButton clickPrevious = new JButton("Previous"); //creates Previous button JButton clickNext = new JButton("Next"); //creates Next button JButton clickDelete = new JButton("Delete"); //creates Delete button JTextField FirstNameInput = new JTextField(""); //creates NameInput text field JTextField LastNameInput = new JTextField(""); JTextField AddressLine1Input = new JTextField(""); JTextField AddressLine2Input = new JTextField(""); JTextField CityInput = new JTextField(""); JTextField StateInput = new JTextField(""); JTextField ZipInput = new JTextField(""); JTextField BirthdayInput = new JTextField(""); JTextField GradDateInput = new JTextField(""); JTextField GpaInput = new JTextField(""); JTextField CreditHoursInput = new JTextField(""); public StudentContainer() { super("STUDENT DATABASE"); //container header is "STUDENT DATABASE" newGUI(); //calls newGUI to initialize GUI readFile(); updateScreen(); } // end constructor{ private void newGUI(){ this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //exit on close statement Container p = this.getContentPane(); //declare container p p.setLayout(new BorderLayout()); //set p to BorderLayout Panel pCenter = new Panel(); //creates pCenter panel pCenter.setLayout(new GridLayout(0,2)); //initiates grid layout pCenter.add(lblFirstName); //adds Name label to pCenter pCenter.add(FirstNameInput); //adds Phone text field to pCenter pCenter.add(lblLastName); //adds Name label to pCenter pCenter.add(LastNameInput); //adds Phone text field to pCenter pCenter.add(lblAddressLine1); //adds Name text field to pCenter pCenter.add(AddressLine1Input); //adds Phone text field to pCenter pCenter.add(lblAddressLine2); //adds Name label to pCenter pCenter.add(AddressLine2Input); //adds Phone text field to pCenter pCenter.add(lblCity); //adds Name label to pCenter pCenter.add(CityInput); //adds Phone text field to pCenter pCenter.add(lblState); //adds Name label to pCenter pCenter.add(StateInput); //adds Phone text field to pCenter pCenter.add(lblZip); //adds Name label to pCenter pCenter.add(ZipInput); //adds Phone text field to pCenter pCenter.add(lblBirthday); //adds Name label to pCenter pCenter.add(BirthdayInput); //adds Phone text field to pCenter pCenter.add(lblGradDate); //adds Name label to pCenter pCenter.add(GradDateInput); //adds Phone text field to pCenter pCenter.add(lblGpa); //adds Name label to pCenter pCenter.add(GpaInput); //adds Phone text field to pCenter pCenter.add(lblCreditHours); //adds Name label to pCenter pCenter.add(CreditHoursInput); //adds Phone text field to pCenter Panel pSouth = new Panel(); //creates pSouth panel pSouth.add(clickPrevious); //adds previous button pSouth.add(clickSave); //adds save button pSouth.add(clickDelete); //adds delete button pSouth.add(clickNext); //adds next button p.add(pCenter, BorderLayout.CENTER); //adds pCenter panel to container p.add(pSouth, BorderLayout.SOUTH); //adds pSouth panel to container clickPrevious.addActionListener(this); //assigns ActionListener to previous button this.pack(); this.setVisible(true); clickSave.addActionListener(this); //assigns ActionListener to save button this.pack(); this.setVisible(true); clickDelete.addActionListener(this); //assigns ActionListener to delete button this.pack(); this.setVisible(true); clickNext.addActionListener(this); //assigns ActionListener to next button this.pack(); }//end newGUI public void actionPerformed(ActionEvent e) { // if (e.getSource() == clickSave){ //if save button is clicked, save unless there is nothing to save // String x = NameInput.getText(); // String y = AddressInput.getText(); // String z = PhoneInput.getText(); // // if(x.equals("")){ //if name is blank // JOptionPane.showMessageDialog(null, "Name must be entered!"); //display error message // }//end else if // else if(y.equals("")){ //if address is blank // JOptionPane.showMessageDialog(null, "Address must be entered!"); //display error message // }//end else if // else if(z.equals("")){ //if phone number is blank // JOptionPane.showMessageDialog(null, "Phone Number must be entered!"); //display error message // }//end else if // else{ //if all items are entered // Name.add(NameInput.getText()); //updates list with Name // Address.add(AddressInput.getText()); //updates list with Address // Phone.add(PhoneInput.getText()); //updates list with Phone // WriteFile(); //calls WriteFile to save to the text file // counter = Name.size() - 1; //takes counter to last record of list // JOptionPane.showMessageDialog(null, "Save is complete!"); //display message to confirm save to user // }//end else // // }//end if // // else if (e.getSource() == clickDelete){ //if delete is clicked, delete unless there is nothing to delete // if(counter >= 0){ //if the counter is greater than 0 // Name.remove(counter); //remove name from name list // Address.remove(counter); //remove address from address list // Phone.remove(counter); //remove phone number from phone list // WriteFile(); //deletes record from text file // if (counter == Name.size()){ //if counter was last record // if (Name.size() == 0) //if size is empty then counter becomes -1 // counter = -1; // else // counter = counter - 1; //sets counter to previous record // } // updateScreen(); //call update method to display blank text fields // JOptionPane.showMessageDialog(null, "Entry has been Deleted!"); //display message to confirm save to user // }//end if // else { // BlankGUI(); //if counter is not greater than 0 // JOptionPane.showMessageDialog(null, "There are no records!"); //display error message to user // }//end else // }//end else if // // else if (e.getSource() == clickPrevious){ //if previous is clicked, go to previous unless there is no previous // if(counter > 0){ //if counter is greater than 0 // counter--; // updateScreen(); //call update method to display blank form // } // else // JOptionPane.showMessageDialog(null, "This is the first record"); //display message to user // }//end else if // // else if (e.getSource() == clickNext){ //if next is clicked, go to next unless there is nothing next. if (e.getSource() == clickNext){ FirstNameInput.setText(students[counter].firstName); } // if(counter > -1){ //if the arrayList is not empty // // if (counterRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.