-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataDisplay.java
More file actions
149 lines (96 loc) · 3.13 KB
/
dataDisplay.java
File metadata and controls
149 lines (96 loc) · 3.13 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*FILE Viewer or more like any data window standalone viewer
Reminds me of Yahoo Chat box back in the day */
//Stand alone
//Author: M.Brohi
import java.awt.*;
import java.awt.event.*;
import java.io.*;
/* Class will create a GUI */
public class FileViewer extends Frame implements ActionListener {
String directory;
TextArea textarea;
public FileViewer() { this(null, null);}
public FileViewer(String filename) {this(null, filename); }
public FileViewer(String directory, String filename) {
super();
addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e) {dispose (); }
});
textarea = new TextArea("", 24, 80);
textarea.setFont(new Font( "Monospaced", Font.PLAIN, 12));
textarea.setEditable(false);
this.add(textarea, "Center");
Panel p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 5));
this.add(p, "South");
Font font = new Font("SansSerif", Font.BOLD, 14);
Button openfile = new Button("Open File");
Button close = new Button("Close");
openfile.addActionListener(this);
openfile.setActionCommand("open");
openfile.setFont(font);
close.addActionListener(this);
close.setActionCommand("close");
close.setFont(font);
p.add(openfile);
p.add(close);
this.pack();
//Directory From Filename or Current Directory
if (directory == null) {
File f;
if ((filename != null) && (f = new File (filename)) .isAbsolute()) {
directory = f.getParent();
filename = f.getName();
}
else directory = System.getProperty("user.dir");
}
this.directory = directory;
setFile(directory, filename);
}
public void setFile(String directory, String filename) {
if (( filename == null) || (filename.length() == 0))
return;
File f;
FileReader in = null;
try {
f = new File(directory, filename);
in = new FileReader(f);
char[] buffer = new char[4096];
int len;
textarea.setText("");
while((len = in.read(buffer)) != -1) {
String s = new String(buffer, 0 ,len);
textarea.append(s);
}
this.setTitle("FileViewer: " + filename);
textarea.setCaretPosition(0);
}
catch (IOException e) {
textarea.setText(e.getClass().getClass().getName() + ": " + e.getMessage());
this.setTitle("FileViewer: " + filename + " : I/O Exception");
}
finally { try { if (in!= null) in.close(); } catch (IOException e) {} }
}
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.contentEquals("open")) {
FileDialog f = new FileDialog(this, "Open File", FileDialog.LOAD);
f.setDirectory(directory);
f.show();
directory = f.getDirectory();
setFile(directory, f.getFile());
f.dispose();
}
else if (cmd.equals ("close"))
this.dispose();
}
static public void main(String[] args) throws IOException {
Frame f = new FileViewer ((args.length == 1) ?args[0]:null);
f.addWindowListener(new WindowAdapter() {
public void windowClosed( WindowEvent e) {
System.exit(0);}
});
f.show();
}
}