public class StudentMarks6 extends JFrame
class ButtonHandler implements ActionListener
class MarkComparator implements Comparator
1: //StudentMarks6.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 StudentMarks5.java by activating the "Sort by Mark" button to
5: //permit the user to sort the displayed names and marks by descending mark.
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.ArrayList;
14: import java.util.Collections;
15: import java.util.Iterator;
16: import java.util.Scanner;
17: import javax.swing.JButton;
18: import javax.swing.JFrame;
19: import javax.swing.JOptionPane;
20: import javax.swing.JPanel;
21: import javax.swing.JScrollPane;
22: import javax.swing.JTextArea;
24: //New interface used:
25: import java.util.Comparator; //another "interface"
27: public class StudentMarks6 extends JFrame
28: {
29: private ArrayList<String> studentList;
30: private JTextArea display;
31: private JPanel buttonPanel;
32: private JButton fileButton;
33: private JButton byNameButton;
34: private JButton byMarkButton;
35: private JButton quitButton;
36: private Font font;
38: public StudentMarks6(String title)
39: {
40: super(title);
42: studentList = new ArrayList<String>();
44: display = new JTextArea(15, 30);
45: font = new Font("Monospaced", Font.PLAIN, 16);
46: display.setFont(font);
48: buttonPanel = new JPanel();
49: fileButton = new JButton("Load File");
50: byNameButton = new JButton("Sort by Name");
51: byMarkButton = new JButton("Sort by Mark");
52: quitButton = new JButton("Quit");
54: ButtonHandler buttonHandler = new ButtonHandler();
55: quitButton.addActionListener(buttonHandler);
56: fileButton.addActionListener(buttonHandler);
57: byNameButton.addActionListener(buttonHandler);
58:
59: //New code to connect the button handler to the Sort by Mark button:
60: byMarkButton.addActionListener(buttonHandler);
62: buttonPanel.add(fileButton);
63: buttonPanel.add(byNameButton);
64: buttonPanel.add(byMarkButton);
65: buttonPanel.add(quitButton);
67: add(new JScrollPane(display), BorderLayout.CENTER);
68: add(buttonPanel, BorderLayout.SOUTH);
69: }
71: //Note that this method now has a parameter:
72: public void getDataFromFile(String fileName)
73: {
74: display.setText("");
75: studentList.clear();
76: try
77: {
78: Scanner in = new Scanner(new File(fileName));
79: String line;
80: while (in.hasNext())
81: {
82: line = in.nextLine();
83: display.append(line + "\n");
85: //New: Student name and mark added to studentList.
86: studentList.add(line);
87: }
88: in.close();
89: display.setCaretPosition(0);
90: }
91: catch (FileNotFoundException e)
92: {
93: JOptionPane.showMessageDialog(null,
94: "File not found.\nTry again.");
95: }
96: }
98: class ButtonHandler implements ActionListener
99: {
100: public void actionPerformed(ActionEvent e)
101: {
102: if (e.getSource() == quitButton)
103: System.exit(0);
105: if (e.getSource() == fileButton)
106: {
107: String fileName = JOptionPane.showInputDialog(
108: "What is the name of the data file?");
109: getDataFromFile(fileName);
110: }
111:
112: if (e.getSource() == byNameButton)
113: {
114: display.setText("");
115: ArrayList<String> tempStudentList =
116: new ArrayList<String>(studentList);
117: Collections.sort(tempStudentList);
118: Iterator<String> i = tempStudentList.iterator();
119: while (i.hasNext()) display.append(i.next() + "\n");
120: display.setCaretPosition(0);
121: }
123: //New: What to do if the user clicks the Sort by Mark button.
124: if (e.getSource() == byMarkButton)
125: {
126: display.setText("");
127: ArrayList<String> tempStudentList =
128: new ArrayList<String>(studentList);
129: Collections.sort(tempStudentList, new MarkComparator());
130: Iterator<String> i = tempStudentList.iterator();
131: while (i.hasNext()) display.append(i.next() + "\n");
132: display.setCaretPosition(0);
133: }
134: }
135: }
137: //New: The "Comparator" class that determines how the strings will be
138: //sorted when an object of its type is used as the second parameter to
139: //the ArrayList.sort() algorithm.
140: class MarkComparator implements Comparator<String>
141: {
142: public int compare(String s1, String s2)
143: {
144: return -1 * s1.substring(18).compareTo(s2.substring(18));
145: }
146: }
148: public static void main(String[] args)
149: {
150: StudentMarks6 app = new StudentMarks6("Record of Student Marks");
151: app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
152: app.pack();
153: app.setVisible(true);
154: }
155: }