Java Reference
In-Depth Information
}
sketchChanged = false; // Set as unchanged
}
The saveSketch() method writes the current SketchModel object to the object output stream that
we create from the File object that is passed to it. If an error occurs, an exception of type
IOException will be thrown, in which case we write the exception to the standard error output
stream, and pop up a dialog indicating that an error has occurred. We assume the user might want to
retry the operation so we just return rather than terminate the application.
If the write operation succeeds, then we need to consider whether the data members in the window
object relating to the file need to be updated. This will be the case when the File object passed to the
saveSketch() method is not the same as the reference in modelFile . If this is so, we update the
modelFile and filename members of the window object, and set the window title to reflect the new
file name and path. In any event, the sketchChanged flag is reset to false , as the sketch is now
safely stored away in the file.
We can now put together the code to handle the S ave menu item event.
Try It Out - Saving a Sketch
The code to handle the S ave menu item event will go in the actionPerformed() method of the
inner class FileAction . We have done all the work, so it amounts to just one statement:
public void actionPerformed(ActionEvent e) {
String name = (String)getValue(NAME);
if(name.equals(saveAction.getValue(NAME))) {
saveOperation();
}
// Plus the rest of the code for the method...
}
You can recompile Sketcher and run it again. The S ave menu item and toolbar button should now be
working. When you select either of them, you should get the dialog displayed below. This version has the
look-and-feel of Windows of course. If you are using a different operating system it won't be exactly the same.
Search WWH ::




Custom Search