21.5. Summary

In this chapter you have added full support for the File menu to the Sketcher application for both sketch storage and retrieval and for printing. You should find that the techniques that you have used here are readily applicable in other Java applications. The approach to saving and restoring a model object is not usually dependent on the kind of data it contains. Of course, if your application is a word processor, you will have a little more work to do taking care that the number of lines printed on each page is a whole number of lines. In other words, you will have to make sure you avoid having the top half of a line of text on one page and the bottom half on the next. There are other Java classes to help with that, however, and I don't really have the space to discuss them here—but look them up—the javax.swing.text package is a veritable gold mine for text handling!

If you have been following all the way with Sketcher, you now have an application that consists of well over 1500 lines of code, so you should be pretty pleased with yourself. And you're not finished with Sketcher yet—you'll add the capability to export and import sketches in XML over the next two chapters.

The important points I've covered in this chapter are:

  • You can implement writing your model object to a file and reading it back by making it serializable.

  • The JFileChooser class provides a generalized way for displaying a dialog to enable a file to be chosen.

  • A printing operation is initiated by ...

Get Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.