|
| 1 | +import java.awt.*; |
| 2 | +import java.awt.event.*; |
| 3 | +import javax.swing.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | +public class TextEdit { |
| 7 | + private JFrame frame; //The frame for everything to sit on. |
| 8 | + private JPanel panel; //The collection of child items. |
| 9 | + private JLabel label; //The "enter file" label. |
| 10 | + private JTextField textfield; //The file name text area. |
| 11 | + private JTextArea textarea; //The file display area. |
| 12 | + private JButton save, load; //The file open / close buttons. |
| 13 | + |
| 14 | + public TextEdit() { |
| 15 | + //Sets window label. |
| 16 | + frame = new JFrame("Score Display"); |
| 17 | + |
| 18 | + //Close window correctly if prompted. |
| 19 | + frame.addWindowListener(new WindowAdapter() { |
| 20 | + public void windowClosing(WindowEvent e) { |
| 21 | + System.exit(0); |
| 22 | + } |
| 23 | + }); |
| 24 | + |
| 25 | + |
| 26 | + label = new JLabel("Enter file name."); |
| 27 | + textfield = new JTextField(25); |
| 28 | + textarea = new JTextArea(20,30); |
| 29 | + textarea.setBackground(Color.white); |
| 30 | + |
| 31 | + save = new JButton("Save"); |
| 32 | + save.addActionListener(new Listener()); |
| 33 | + load = new JButton("Load"); |
| 34 | + load.addActionListener(new Listener()); |
| 35 | + |
| 36 | + panel = new JPanel(); |
| 37 | + panel.setPreferredSize(new Dimension(500,500)); |
| 38 | + panel.setBackground(Color.gray); |
| 39 | + panel.add(label); |
| 40 | + panel.add(textfield); |
| 41 | + panel.add(save); |
| 42 | + panel.add(load); |
| 43 | + panel.add(textarea); |
| 44 | + frame.getContentPane().add(panel); |
| 45 | + } |
| 46 | + public void display() { |
| 47 | + frame.pack(); |
| 48 | + frame.show(); |
| 49 | + } |
| 50 | + private class Listener implements ActionListener { |
| 51 | + public void actionPerformed( ActionEvent event) { |
| 52 | + String file = textfield.getText(), line = "", all = ""; |
| 53 | + Object source = event.getSource(); |
| 54 | + |
| 55 | + if(source == load) { |
| 56 | + try { |
| 57 | + FileInputStream stream = new FileInputStream(file); |
| 58 | + InputStreamReader ISReader = new InputStreamReader(stream); |
| 59 | + BufferedReader reader = new BufferedReader(ISReader); |
| 60 | + line = reader.readLine(); |
| 61 | + while (line != null) { |
| 62 | + String[] data = line.split(" "); |
| 63 | + for (int i = 0; i < data.length; i++) { |
| 64 | + all += data[i] + " "; |
| 65 | + } |
| 66 | + line = reader.readLine(); |
| 67 | + all += "\n"; |
| 68 | + } |
| 69 | + textarea.setText(all); |
| 70 | + stream.close(); |
| 71 | + } |
| 72 | + catch (IOException exception) { |
| 73 | + System.out.println("Error opening file\n" + exception); |
| 74 | + } |
| 75 | + } |
| 76 | + if (source == save) { |
| 77 | + try { |
| 78 | + FileOutputStream stream = new FileOutputStream(file); |
| 79 | + PrintWriter pw = new PrintWriter(stream, true); |
| 80 | + pw.println(textarea.getText()); |
| 81 | + stream.close(); |
| 82 | + } |
| 83 | + catch(IOException exception) { |
| 84 | + System.out.println("File error" + exception.toString() ); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments