my previous button does go to the previous file:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class StudentAddress extends JPanel
{
private JButton submit, clear, next, display, previous;
private JLabel first, last, street, town,state, zip;
private JTextField fName, lName, hStreet,hTown,hState, hZip;
private int studentCount=0, displayCount=0, i; //studentCount the number of students
private Student [] studentArray = new Student[100];
private boolean fileError = false;
//constructor
public StudentAddress(){
//set Layout of this panel
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
//create all the labels
first = new JLabel("First Name:");
last = new JLabel("Last Name:");
street = new JLabel("Street:");
town = new JLabel("Town:");
zip = new JLabel ("Zip:");
state = new JLabel("State:");
//create all the text fields
fName = new JTextField( "John", 20);
lName = new JTextField( "Doe", 20);
hStreet = new JTextField("Houston St.", 20);
hTown = new JTextField("Itta Bena",10);
hZip = new JTextField("1222",5);
hState = new JTextField("MS",2);
//Layout the components in the panel
JPanel fPanel = new JPanel();
fPanel.setLayout(new BoxLayout(fPanel, BoxLayout.X_AXIS));
fPanel.add(first);
JPanel panel1 = new JPanel();
panel1.add(fName);
fPanel.add(panel1);
JPanel lPanel = new JPanel();
lPanel.setLayout(new BoxLayout(lPanel, BoxLayout.X_AXIS));
lPanel.add(last);
JPanel panel2 = new JPanel();
panel2.add(lName);
lPanel.add(panel2);
JPanel sPanel= new JPanel();
sPanel.setLayout(new BoxLayout(sPanel, BoxLayout.X_AXIS));
sPanel.add(street);
JPanel panel3 = new JPanel();
panel3.add(hStreet);
sPanel.add(panel3);
JPanel tszPanel = new JPanel();
tszPanel.setLayout(new BoxLayout(tszPanel, BoxLayout.X_AXIS));
JPanel panel4 = new JPanel();
tszPanel.add(town); panel4.add(hTown); tszPanel.add(panel4);
JPanel panel5 = new JPanel();
tszPanel.add(state); panel5.add(hState); tszPanel.add(panel5);
JPanel panel6 = new JPanel();
tszPanel.add(zip); panel6.add(hZip); tszPanel.add(panel6);
//create the buttons
submit = new JButton("Submit");
clear = new JButton("Clear");
display = new JButton("Display");
next = new JButton("Next >>");
next.setEnabled(true);
previous = new JButton("Previous <<");
previous.setEnabled(true);
//add all the components
add(fPanel);
add(lPanel);
add(sPanel);
add(tszPanel);
//create a panel to hold all buttons
JPanel buttons = new JPanel();
buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
buttons.add(submit);
buttons.add(clear);
buttons.add(display);
buttons.add(next);
buttons.add(previous);
//add the action listener
submit.addActionListener(new ButtonListener());
clear.addActionListener(new ButtonListener());
next.addActionListener(new ButtonListener());
previous.addActionListener(new ButtonListener());
display.addActionListener(new ButtonListener());
//add the panel with all the three buttons
add(buttons);
//set the size for this panel
setPreferredSize(new Dimension(400,225));
setBackground(Color.yellow);
fName.setText("");
lName.setText("");
hStreet.setText("");
hTown.setText("");
hZip.setText("");
hState.setText("");
}
private class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
String fname="", lname="", street="", town="", state="", zip="";
Student newStudent;
long zipCode;
Address newSaddress, newHaddress;
if (event.getSource() == submit){
System.out.println("Submit Button Pushed");
if ( !(fName.getText().equals("") || lName.getText().equals("")
|| hStreet.getText().equals("")|| hTown.getText().equals("")
|| hZip.getText().equals("")|| hState.getText().equals("")) ){
//get the data from the text boxes
fname = fName.getText();
lname = lName.getText();
street = hStreet.getText();
town = hTown.getText();
zip = hZip.getText();
//convert the string to long
zipCode = Long.parseLong(zip);
state = hState.getText();
//create a school and home address
newSaddress = new Address(street, town, state, zipCode);
newHaddress = new Address(street, town, state, zipCode);
//create a student with the information from text boxes
newStudent = new Student(fname, lname, newSaddress, newHaddress);
//store it in the file "student.txt"
newStudent.appendToFile("student.txt");
System.out.println("Information appended to the data file");
} //end of if
else System.out.println("Error: One or more fields are empty");
}
else if (event.getSource() == next){
System.out.println("Next Button Pushed");
System.out.println("There are "+displayCount+" students in the file");
//Once the all students as displayed
//pushing the next button will just show the last student
i=0;
if (displayCount < studentCount){
fName.setText(studentArray[displayCount].getFirstName());
lName.setText(studentArray[displayCount].getLastName());
hStreet.setText(studentArray[displayCount].getHomeAddress().getStreet());
hTown.setText(studentArray[displayCount].getHomeAddress().getTown());
hZip.setText(""+studentArray[displayCount].getHomeAddress().getZip());
hState.setText(studentArray[displayCount].getHomeAddress().getState());
displayCount++;
}
else next.setEnabled(true);
}
else if (event.getSource() ==previous){
System.out.println("Previous Button Pushed");
System.out.println("There are "+displayCount+" students in the file");
//Once the all students as displayed
//pushing the next button will just show the last student
i=1;
if (studentCount < displayCount){
fName.setText(studentArray[displayCount].getFirstName());
lName.setText(studentArray[displayCount].getLastName());
hStreet.setText(studentArray[displayCount].getHomeAddress().getStreet());
hTown.setText(studentArray[displayCount].getHomeAddress().getTown());
hZip.setText(""+studentArray[displayCount].getHomeAddress().getZip());
hState.setText(studentArray[displayCount].getHomeAddress().getState());
displayCount--;
}
else previous.setEnabled(true);
}
else if (event.getSource() == clear){
System.out.println("Clear Button Pushed");
fName.setText("");
lName.setText("");
hStreet.setText("");
hTown.setText("");
hZip.setText("");
hState.setText("");
}
else if (event.getSource() == display){
displayCount = 0;
fileError = false;
next.setEnabled(true);
previous.setEnabled(true);
System.out.println("Display Button is Pushed");
readFromFile("student.txt");
//Display error message if no students are found
if(fileError) System.out.println("No students to display");
else
System.out.println("There are" + (displayCount+1)+" students");
/*for (int index = 0; index < studentCount ; index ++)
{
System.out.println((index+1) +" Record");
System.out.println(studentArray[index].getFirstName());
System.out.println(studentArray[index].getLastName());
}*/
//Display the first record
if(i=0)
{
fName.setText(studentArray[displayCount-1].getFirstName());
lName.setText(studentArray[displayCount-1].getLastName());
hStreet.setText(studentArray[displayCount-1].getHomeAddress().getStreet());
hTown.setText(studentArray[displayCount-1].getHomeAddress().getTown());
hZip.setText(""+studentArray[displayCount-1].getHomeAddress().getZip());
hState.setText(studentArray[displayCount-1].getHomeAddress().getState());
//increment to the next record
displayCount--;
}
else if(i=1)
{
fName.setText(studentArray[displayCount].getFirstName());
lName.setText(studentArray[displayCount].getLastName());
hStreet.setText(studentArray[displayCount].getHomeAddress().getStreet());
hTown.setText(studentArray[displayCount].getHomeAddress().getTown());
hZip.setText(""+studentArray[displayCount].getHomeAddress().getZip());
hState.setText(studentArray[displayCount].getHomeAddress().getState());
//increment to the next record
displayCount++;
}
}
}//end of actionPerformed method
}//end of ButtonListener class
//-----------------------------------------------------------------
// Reads from a file.
//-----------------------------------------------------------------
public void readFromFile(String fileName)
{
Student student;
String first="", last="", street="", town="", state="";
long zip=0;
Address homeAddress, schoolAddress;
int index = 0, lineCount = 0;
System.out.println("Reading from file " + fileName);
//set the value of studentCount
studentCount = 0;
try {
BufferedReader in = new BufferedReader(new FileReader(fileName));
String str;
while ((str = in.readLine()) != null)
{
//System.out.println("Beginning to read the info of " +(studentCount+1) + " student");
lineCount++;
//reading the first name
if(lineCount == 1){
first = str;
}
else if (lineCount == 2){
last = str;
}
else if (lineCount == 3){
street = str;
}
else if (lineCount == 4){
town = str;
}
else if (lineCount == 5){
state = str;
}
else if (lineCount == 6){
zip = Long.parseLong(str);
}
if (str.equals("#")){
//System.out.println("Reached the end of " + (studentCount+1) + " student");
//create next dummy student and address in order to store information
homeAddress = new Address("X", "X", "X", 1234);
schoolAddress = new Address("X", "X", "X", 1234);
student = new Student("X","X",homeAddress, schoolAddress);
//store it in the array
studentArray[studentCount] = student;
studentArray[studentCount].setFirstName(first);
studentArray[studentCount].setLastName(last);
studentArray[studentCount].setSchoolAddress(street, town, state, zip);
studentArray[studentCount].setHomeAddress(street, town, state, zip);
studentCount++;
lineCount = 0;
}
//System.out.println(str);
}
//System.out.println("There are "+ studentCount + " students");
in.close();
}
catch (IOException e) {
System.out.println("Error processing file " + fileName);
fileError = true;
}
}//end of method readFromFile
}//end of class Student Address