Creating the Print Dialog Box


You can use the PrintClass.java file to open the default Print dialog box for the Text Editor application. In this dialog box, you can specify the print range, number of copies, and page property before you print the file.

Listing 6-3 shows the contents of the PrintClass.java file:

Listing 6-3: The PrintClass.java File
start example
 /* Imports java.awt.print package class. */ import java.awt.print.PrinterJob; import java.awt.print.PrinterException; import java.awt.print.PageFormat; import java.awt.print.Printable; /* Imports java.awt package classes. */ import java.awt.Component; import java.awt.Graphics; import java.awt.Graphics2D; /* Imports javax.swing package class. */ import javax.swing.RepaintManager; /* class PrintClass - This class implements Printable interface and enable the end user to print the document. Fields:     compPrint - Contains the document that to be printed. Methods:    print() - This method prints the current document opened in the text editor window  */ public class PrintClass implements Printable {    /* Declares the object of Component class. */    private Component compPrint;    public Editor editor;    /* Defines the default constructor. */    public PrintClass(Component compPrint, Editor editor)    {       this.compPrint = compPrint;       this.editor = editor;    }    /*    print() - This method prints the document    Parameters: lse - NA    Return Value: NA    */    public void print()    {       /* Creates an object of the PrinterJob class */       PrinterJob printer = PrinterJob.getPrinterJob();       /* Sets the object of PrinterJob class to printable */       printer.setPrintable(this);       if(printer.printDialog())       {           try          {             /* Prints the document */             printer.print();          }          catch(PrinterException pe)          {             System.out.println("Error: " + pe);          }       }       editor.show();    }    /*    print() - This method opens a Print dialog box    Parameters:     g - Represents the object of Graphics class    format - Represents the object of PageFormat class    index - Represents an index of page    Return Value: int PAGE_EXIST    */    public int print(Graphics g, PageFormat format, int index)    {       if(index > 0)       {          return(NO_SUCH_PAGE);       }       else       {          /*           Creates an object of the Graphics2D class and converts simple graphics to 2D graphics           */          Graphics2D g2d = (Graphics2D)g;          /*           Translates the origin of the Graphics2D context to the point (x, y)           in the current coordinate system           */          g2d.translate(format.getImageableX(), format.getImageableY());          /* Creates the object of the RepaintManager class */           RepaintManager manager1 = RepaintManager.currentManager(compPrint);          /* Sets the double buffer to FALSE */          manager1.setDoubleBufferingEnabled(false);          /* Paints the component */          compPrint.paint(g2d);          /* Creates the object of RepaintManager class */           RepaintManager manager2 = RepaintManager.currentManager(compPrint);          /* Sets the double buffer to TRUE */          manager2.setDoubleBufferingEnabled(true);          /* Returns the PAGE_EXIST value */          return(PAGE_EXISTS);       }    }  } 
end example
 

Download this Listing .

In the above code, the PrintClass() constructor of the PrintClass takes two arguments, compPrint and editor. The compPrint argument is an object of the Component class that contains the printing content. The editor argument is an object of the Editor class.

The print() method creates an object of the PrinterJob class. This method then sets the printable property on the object of the PrinterClass to open the Print dialog box, as shown in Figure 6-3:

click to expand: this figure shows the print dialog box, which displays three panels: printer, print range, and copies.
Figure 6-3: The Print Dialog Box

The Printer panel in the Print dialog box displays the status of the printer selected by end users in the Name combo box. The Properties button in this panel helps set the layout and quality of the page. In the Print range panel, end users can select a radio button to specify the print range. The Copies panel provides a list box that allows end users to specify the number of copies of a document they want to print.

When end users click the OK button in the Print dialog box, the print() method of PrinterJob class is invoked. This method prints the file.

If end users click the Cancel button of the Print Dialog box, the editor.show() method is invoked. This method closes the Print dialog box and sets the focus to the main window of the Text Editor application.

If end users click the Properties button of the Help dialog box, the print(Graphics g, PageFormat format, int index) method is invoked. This method uses three objects as parameters: g object of the Graphics class, format object of the PageFormat class, and index object of the Integer class. The print() method creates the object of the Graphics2D class. The object of the Graphics2D class translates the origin of the Graphics2D context to the point x, y in the current coordinate system. The print() method then creates the object of the RepaintManager class and sets the double buffer property to false. Next, the print() method paints the component using the paint() method. The print() method again creates the object of the RepaintManager class and sets the double buffer property to TRUE. Finally, the print() method returns the PAGE_EXISTS variable.




Java InstantCode. Developing Applications Using Java NIO
Java InstantCode. Developing Applications Using Java NIO
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 55

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net