-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathFileReadApplet.java
More file actions
72 lines (66 loc) · 1.89 KB
/
Copy pathFileReadApplet.java
File metadata and controls
72 lines (66 loc) · 1.89 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
package signed;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.nio.file.*;
import javax.swing.*;
/**
* This applet can run "outside the sandbox" and read local files when it is given the right
* permissions.
* @version 1.12 2012-06-10
* @author Cay Horstmann
*/
public class FileReadApplet extends JApplet
{
private JTextField fileNameField;
private JTextArea fileText;
public void init()
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
fileNameField = new JTextField(20);
JPanel panel = new JPanel();
panel.add(new JLabel("File name:"));
panel.add(fileNameField);
JButton openButton = new JButton("Open");
panel.add(openButton);
ActionListener listener = new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
loadFile(fileNameField.getText());
}
};
fileNameField.addActionListener(listener);
openButton.addActionListener(listener);
add(panel, "North");
fileText = new JTextArea();
add(new JScrollPane(fileText), "Center");
}
});
}
/**
* Loads the contents of a file into the text area.
* @param filename the file name
*/
public void loadFile(String filename)
{
fileText.setText("");
try
{
fileText.append(new String(Files.readAllBytes(Paths.get(filename))));
}
catch (IOException ex)
{
fileText.append(ex + "\n");
}
catch (SecurityException ex)
{
fileText.append("I am sorry, but I cannot do that.\n");
fileText.append(ex + "\n");
ex.printStackTrace();
}
}
}