/** * StudentList.java * * @author Mr J * @version 20050209 */ import java.applet.*; import java.awt.*; import java.awt.event.*; public class StudentList extends Applet implements ActionListener { // Using a defined StudentRecordNode class private StudentRecordNode root; // first item in list; TextArea textWindow = new TextArea("This is where the outputs will appear\n", 20, 30); // fields and labels for data entry Label nameLabel = new Label("Student name: "); Label tutorLabel = new Label("Tutor group: "); TextField nameField = new TextField(30); TextField tutorField = new TextField( 3); // action buttons Button addButton = new Button("Add"); Button lstButton = new Button("List"); /** * This method sets uo the GUI objects */ public void init() { addButton.addActionListener(this); lstButton.addActionListener(this); // text objects add(nameLabel); add(nameField); add(tutorLabel); add(tutorField); add(addButton); add(lstButton); add(textWindow); } /** * This method detects button events * * @param ActionEvent - the action that triggered the call to this method */ public void actionPerformed(ActionEvent e) { // React to the button presses if (e.getSource() == addButton) { addNode(); } else { displayList(); } } /** * Adds a node to the head of the linked list */ public void addNode() { // create a new node using the constructor of Class StudentRecordNode // this node has its reference field pointed to root StudentRecordNode theNode = new StudentRecordNode( nameField.getText(), tutorField.getText(), root ); // now we point the root pointer to this node root = theNode; // clear the fields of text for convenience nameField.setText(""); tutorField.setText(""); // put the cursor in the name field nameField.requestFocus(); } /** * A method to display the linked list of nodes in the text area window */ public void displayList() { // a temporary pointer to hold the reference of each node of the list in turn StudentRecordNode temp; int numInList=1; // start at thr root temp = root; textWindow.append( "\n head of list \n ------------\n" ); // "walk" down the list, using the temp pointer while (temp != null) { textWindow.append( "Number: " + numInList++ + "\n" ); textWindow.append( "Name: " + temp.getName() + "\n" ); textWindow.append( "Tutor: " + temp.getTutor() + "\n" ); textWindow.append( "\n--------------------\n" ); temp = temp.getNext(); } } }