Source of StudentMarks5.java


  1: //StudentMarks5.java
  2: //Loads student names and marks from a textfile chosen by the user and
  3: //displays them in a JTextArea component on the screen.
  4: //Extends StudentMarks4.java by activating the "Sort by Name" button to
  5: //permit the user to sort the displayed names alphabetically by last name.

  7: import java.awt.BorderLayout;
  8: import java.awt.Font;
  9: import java.awt.event.ActionEvent;
 10: import java.awt.event.ActionListener; //an "interface"
 11: import java.io.File;
 12: import java.io.FileNotFoundException;
 13: import java.util.Scanner;
 14: import javax.swing.JButton;
 15: import javax.swing.JFrame;
 16: import javax.swing.JOptionPane;
 17: import javax.swing.JPanel;
 18: import javax.swing.JScrollPane;
 19: import javax.swing.JTextArea;

 21: //New classes used:
 22: import java.util.ArrayList;
 23: import java.util.Collections;
 24: import java.util.Iterator;

 26: public class StudentMarks5 extends JFrame
 27: {
 28:     //New private data member:
 29:     private ArrayList<String> studentList;

 31:     private JTextArea display;
 32:     private JPanel buttonPanel;
 33:     private JButton fileButton;
 34:     private JButton byNameButton;
 35:     private JButton byMarkButton;
 36:     private JButton quitButton;
 37:     private Font font;

 39:     public StudentMarks5(String title)
 40:     {
 41:         super(title);

 43:         //New: Initialize the student list.
 44:         studentList = new ArrayList<String>();

 46:         display = new JTextArea(15, 30);

 48:         font = new Font("Monospaced", Font.PLAIN, 16);
 49:         display.setFont(font);

 51:         buttonPanel = new JPanel();
 52:         fileButton = new JButton("Load File");
 53:         byNameButton = new JButton("Sort by Name");
 54:         byMarkButton = new JButton("Sort by Mark");
 55:         quitButton = new JButton("Quit");

 57:         ButtonHandler buttonHandler = new ButtonHandler();
 58:         quitButton.addActionListener(buttonHandler);
 59:         fileButton.addActionListener(buttonHandler);
 60: 
 61:         //New code to connect the button handler to the Sort by Name button:
 62:         byNameButton.addActionListener(buttonHandler);

 64:         buttonPanel.add(fileButton);
 65:         buttonPanel.add(byNameButton);
 66:         buttonPanel.add(byMarkButton);
 67:         buttonPanel.add(quitButton);

 69:         add(new JScrollPane(display), BorderLayout.CENTER);
 70:         add(buttonPanel, BorderLayout.SOUTH);
 71:     }

 73:     //Note that this method now has a parameter:
 74:     public void getDataFromFile(String fileName)
 75:     {
 76:         display.setText("");

 78:         //New: studentList set to empty. Why?
 79:         studentList.clear();
 80:         try
 81:         {
 82:             Scanner in = new Scanner(new File(fileName));
 83:             String line;
 84:             while (in.hasNext())
 85:             {
 86:                 line = in.nextLine();
 87:                 display.append(line + "\n");

 89:                 //New: Student name and mark added to studentList.
 90:                 studentList.add(line);
 91:             }
 92:             in.close();
 93:             display.setCaretPosition(0);
 94:         }
 95:         catch (FileNotFoundException e)
 96:         {
 97:             JOptionPane.showMessageDialog(null,
 98:                 "File not found.\nTry again.");
 99:         }
100:     }

102:     class ButtonHandler implements ActionListener
103:     {
104:         public void actionPerformed(ActionEvent e)
105:         {
106:             if (e.getSource() == quitButton)
107:                 System.exit(0);

109:             if (e.getSource() == fileButton)
110:             {
111:                 String fileName = JOptionPane.showInputDialog(
112:                     "What is the name of the data file?");
113:                 getDataFromFile(fileName);
114:             }
115: 
116:             //New: What to do if the user clicks the Sort by Name button.
117:             if (e.getSource() == byNameButton)
118:             {
119:                 display.setText("");
120:                 ArrayList<String> tempStudentList =
121:                     new ArrayList<String>(studentList);
122:                 Collections.sort(tempStudentList);
123:                 Iterator<String> i = tempStudentList.iterator();
124:                 while (i.hasNext()) display.append(i.next() + "\n");
125:                 display.setCaretPosition(0);
126:             }
127:         }
128:     }

130:     public static void main(String[] args)
131:     {
132:         StudentMarks5 app = new StudentMarks5("Record of Student Marks");
133:         app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
134:         app.pack();
135:         app.setVisible(true);
136:     }
137: }