-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextEditorApp.java
More file actions
118 lines (101 loc) · 3.62 KB
/
TextEditorApp.java
File metadata and controls
118 lines (101 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package module6.exceptions_and_io.texteditorapp;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
/**
* A simple GUI text editor demonstrating File I/O operations
* @author Mae Morella
*/
public class TextEditorApp extends JFrame {
private static final long serialVersionUID = 3616427372788490052L;
private JTextField pathField = new JTextField("./hello_world.txt", 40);
private JButton loadButton = new JButton("Load");
private JButton saveButton = new JButton("Save");
private JTextArea textField = new JTextArea("Enter a path and click 'Load' to get started.");
private JLabel statusBar = new JLabel("Enter a path and click 'Load' to get started.");
public static void main(String[] args) {
new TextEditorApp();
}
public TextEditorApp() {
super("Text Editor - no file loaded");
// Create new Window
setSize(800, 600);
setLayout(new BorderLayout());
// Exit app when window closes.
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// Create and show GUI
JPanel topBar = new JPanel();
topBar.setLayout(new BoxLayout(topBar, BoxLayout.X_AXIS));
topBar.add(pathField);
topBar.add(loadButton);
topBar.add(saveButton);
saveButton.setEnabled(false);
add(topBar, BorderLayout.PAGE_START);
textField.setLineWrap(true);
textField.setEnabled(false);
add(textField, BorderLayout.CENTER);
add(statusBar, BorderLayout.PAGE_END);
setVisible(true);
// Create event handlers
loadButton.addActionListener((event) -> handleLoad());
saveButton.addActionListener((event) -> handleSave());
}
public File getFileFromPath() {
String path = pathField.getText();
return new File(path);
}
public void handleLoad() {
textField.setText("");
File file = getFileFromPath();
if (!file.exists()) {
displayStatus(String.format("%s does not exist. To create it, click 'Save'.", file.getName()));
} else {
try {
textField.setText(readFile(file));
displayStatus(String.format("%s loaded successfully!", file.getName()));
} catch (IOException e) {
displayAlert("Unable to read file at " + file.getPath());
}
}
setTitle("Text Editor - Editing " + file.getName());
textField.setEnabled(true);
saveButton.setEnabled(true);
}
public void handleSave() {
File file = getFileFromPath();
String text = textField.getText();
try {
writeFile(file, text);
} catch (IOException e) {
displayAlert(e.getMessage());
}
}
public String readFile(File f) throws IOException {
String text = null;
try (BufferedReader reader = new BufferedReader(new FileReader(f))) {
text = "";
while (reader.ready()) {
text += reader.readLine() + "\n";
}
}
return text;
}
public void writeFile(File f, String s) throws IOException {
try (BufferedWriter out = new BufferedWriter(new FileWriter(f, false))) {
out.write(s);
displayStatus("Successfully wrote to " + f.getName());
}
}
public void displayStatus(String status) {
statusBar.setText(status);
}
public void displayAlert(String alert) {
statusBar.setText("ERROR: " + alert);
}
}