public class StudentMarks4 extends JFrame
class ButtonHandler implements ActionListener
1: //StudentMarks4.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 StudentMarks3.java by activating the "Load File" button, by
5: //allowing the user to enter the name of a data file, and by displaying
6: //a message if the file is not found.
8: import java.awt.BorderLayout;
9: import java.awt.Font;
10: import java.awt.event.ActionEvent;
11: import java.awt.event.ActionListener; //an "interface"
12: import java.io.File;
13: import java.io.FileNotFoundException;
14: import java.util.Scanner;
15: import javax.swing.JButton;
16: import javax.swing.JFrame;
17: import javax.swing.JPanel;
18: import javax.swing.JScrollPane;
19: import javax.swing.JTextArea;
21: //New class used:
22: import javax.swing.JOptionPane;
24: public class StudentMarks4 extends JFrame
25: {
26: private JTextArea display;
27: private JPanel buttonPanel;
28: private JButton fileButton;
29: private JButton byNameButton;
30: private JButton byMarkButton;
31: private JButton quitButton;
32: private Font font;
34: public StudentMarks4(String title)
35: {
36: super(title);
37: display = new JTextArea(15, 30);
39: font = new Font("Monospaced", Font.PLAIN, 16);
40: display.setFont(font);
42: buttonPanel = new JPanel();
43: fileButton = new JButton("Load File");
44: byNameButton = new JButton("Sort by Name");
45: byMarkButton = new JButton("Sort by Mark");
46: quitButton = new JButton("Quit");
48: ButtonHandler buttonHandler = new ButtonHandler();
49: quitButton.addActionListener(buttonHandler);
51: //New code to connect the button handler to the Load File button:
52: fileButton.addActionListener(buttonHandler);
54: buttonPanel.add(fileButton);
55: buttonPanel.add(byNameButton);
56: buttonPanel.add(byMarkButton);
57: buttonPanel.add(quitButton);
58:
59: add(new JScrollPane(display), BorderLayout.CENTER);
60: add(buttonPanel, BorderLayout.SOUTH);
61: }
63: //Note that this method now has a parameter:
64: public void getDataFromFile(String fileName)
65: {
66: //New: display text set to empty. Why?
67: display.setText("");
68: try
69: {
70: Scanner in = new Scanner(new File(fileName));
71: String line;
72: while (in.hasNext())
73: {
74: line = in.nextLine();
75: display.append(line + "\n");
76: }
77: in.close();
78: display.setCaretPosition(0);
79: }
80: catch (FileNotFoundException e)
81: {
82: //New: Now we do not terminate the program if the input data
83: //file is not found. We give the user another chance.
84: JOptionPane.showMessageDialog(null,
85: "File not found.\nTry again.");
86: }
87: }
89: class ButtonHandler implements ActionListener
90: {
91: public void actionPerformed(ActionEvent e)
92: {
93: if (e.getSource() == quitButton)
94: System.exit(0);
96: //New: What to do if the user clicks the Load File button.
97: if (e.getSource() == fileButton)
98: {
99: String fileName = JOptionPane.showInputDialog(
100: "What is the name of the data file?");
101: getDataFromFile(fileName);
102: }
103: }
104: }
106: public static void main(String[] args)
107: {
108: StudentMarks4 app = new StudentMarks4("Record of Student Marks");
109: app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
110: app.pack();
111: app.setVisible(true);
112: }
113: }