Java Reference
In-Depth Information
The file open process is similar to a save operation, but instead of writing the file, it reads it. You add
another helper method, openSketch() , to the SketcherFrame class that does the reading, given a Path
object identifying the source of the data. Using this method, the code to handle the Open menu item event
in the actionPerformed() method for the FileAction class is:
} else if(this == openAction) {
// Save current sketch if we need to
checkForSave();
// Now open a sketch file
Path file = showDialog(
"Open Sketch File",
// Dialog
window title
"Open", // Button label
"Read a sketch from file", // Button
tooltip text
sketchFilter,
// File filter
null);
// No file
selected
if(file != null) {
// If a file
was selected
if(openSketch(file)) {
// ...then
read it
currentSketchFile = file; // Success!
setTitle(frameTitle + " - " + currentSketchFile);
sketchChanged = false;
}
return;
}
}
Directory "Sketcher 3 opening sketch files"
You can implement the openSketch() method in the SketcherFrame class as follows:
// Method for opening a sketch file and creating the model
public boolean openSketch(Path file) {
try (ObjectInputStream in = new ObjectInputStream(
new
BufferedInputStream(Files.newInputStream(file)))){
theApp.insertModel((SketcherModel)in.readObject());
currentSketchFile = file;
setTitle(frameTitle+" - "+currentSketchFile);
// Update the
window title
Search WWH ::




Custom Search