Java Reference
In-Depth Information
Don't forget to try out the tooltip for the Open button.
How It Works
After dealing with saving the current sketch in the actionPerformed() member of the FileAction
class, you call the showDialog() method that you defined in the SketcherFrame class to display a file
open dialog. The showDialog() method is all-purpose — you can put any kind of label on the button or
any title in the title bar, so you can use it to display all of the dialogs you need for file operations.
If a file was chosen in the dialog that was displayed by the actionPerformed() method, you pass the
Path object that the showDialog() returns to the openSketch() member of the SketcherFrame object
to read a new sketch from the file. The openSketch() method creates an ObjectInputStream object
from the Path object that was passed to it, and reads a SketcherModel object from the stream by calling
the readObject() method. The object returned by the readObject() method has to be cast to the ap-
propriate type — SketcherModel in this case.
You pass this SketcherModel object to the insertModel() method for the application object. This re-
places the current sketch reference in the sketch member of the application object with a reference to the
new sketch, and then sets the view and the application window as observers. Calling repaint() for the
view object displays the new sketch. The paint() method for the view object obtains a reference to the
current model by calling the getModel() member of the application object, which returns the reference
to the new model.
Starting a New Sketch
The File New menu item simply starts a new sketch. This is quite similar to the open operation, except
that you must create an empty sketch rather than read a new one from disk. The processes of checking for
the need to save the current sketch and inserting the new SketcherModel object into the application are the
same.
TRY IT OUT: Implementing the New Operation
Search WWH ::




Custom Search