Main Page

Previous Next


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 we 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 a thousand lines of code, so you should be pretty pleased with yourself.

The important points we have 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 creating a PrinterJob object. This object encapsulates the interface to your printer and is used to manage the printing process.

  • A PrintService object encapsulates a printer.

  • A PageFormat object defines the format for a page, and methods for this object can provide information on the paper size and orientation and the printable area on the page.

  • An object of type Paper defines a page.

  • You can display a print dialog by calling the printDialog() method for a PrinterJob object. The no argument version of printDialog() will display the native print dialog, whereas the version accepting a single argument of type PrintRequestAttributeSet displays a Java print dialog.

  • Printing a page is always done by an object of a class that implements the Printable interface.

  • You print a page by calling methods for the Graphics object passed to the print() method in the Printable interface by the PrinterJob object.

  • You can manage multipage print jobs by implementing the Pageable interface in a class. This will enable different types of class object to be used to print different pages.

  • A Book object can encapsulate a series of pages to be printed. Each Printable object that is appended to a book prints one or more pages in a given format.

Previous Next
JavaScript Editor Java Tutorials Free JavaScript Editor