Open and Save Dialogs
This Thinlet blog contains a few goodiesfor example, how to add an AWT FileDialog to a Thinlet application. Since Thinlet is based on AWT, the trick is to start with the Thinlet instance and then search up in the AWT tree until you find an AWT Frame. Then you hook your file dialog on to the frame:
Container frame = this;
while (!(frame instanceof Frame)) {
frame = frame.getParent();
}
java.awt.FileDialog filedialog = new java.awt.FileDialog(
(Frame)frame,
"Save As ...",
FileDialog.SAVE);
filedialog.show();
String dir = filedialog.getDirectory();
String file = filedialog.getFile();
if (dir != null && file != null) {
File fileSelected = new File(dir, file);
}
If a valid file is selected, you'll get a File instance back, otherwise null. It's now a simple thing to add this code to the Save As... menu choice from the small example above, and then write the contents of the textarea to the file selected:
. . .
if (dir != null && file != null) {
File fileSelected = new File(dir, file);
writeFile(fileSelected);
}
}
. . .
private void writeFile(File file) {
// Get data from text area
Object area = find("area");
String contents = getString(area, "text");
// Split after each newline character
String[] lines = contents.split("\\\\n");
try {
BufferedWriter out = new BufferedWriter(new FileWriter(file));
for (int i = 0; i < lines.length; i++) {
out.write(lines[i]);
}
out.close();
} catch (IOException e) {/* you may use a dialog box here! */}
}
Figure 7 show how it looks when the dialog pops up.
Making a "file open" dialog is now pretty straightforward. Here's the open and read-a-file code:
public void open() {
Container frame = this;
while (!(frame instanceof Frame)) {
frame = frame.getParent();
}
java.awt.FileDialog filedialog = new java.awt.FileDialog((Frame) frame,
"Open from file",
FileDialog.LOAD);
filedialog.show();
String dir = filedialog.getDirectory();
String file = filedialog.getFile();
if (dir != null && file != null) {
File fileSelected = new File(dir, file);
readFile(fileSelected);
}
}
private void readFile(File file) {
StringBuffer s = new StringBuffer();
String str;
try {
BufferedReader in = new BufferedReader(new FileReader(file));
while ((str = in.readLine()) != null) {
if (s.length() > 0) s.append("\n");
s.append(str);
}
in.close();
} catch (IOException e) {/* should be completed */
}
Object area = find("area");
setString(area, "text", s.toString());
}
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|