Anda di halaman 1dari 4

Creating the Print Dialog Box - Java InstantCode: Developing Applicati...

http://book.javanb.com/sillsoft-Press-Java-instantcode-developing-appl...

Display Applications ECDIS SDK:compliant, rapid & secure development for nautical chart apps
www.SevenCs.com

: > Java InstantCode: Developing Applications Using Java NIO

Creating the Print Dialog Box - Java InstantCode: Developing Applications Using Java NIO

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 /* Imports java.awt.print package class. */ import java.awt.print.PrinterJob; import java.awt.print.PageFormat; import java.awt.print.Printable; import java.awt.Component; import java.awt.Graphics; import java.awt.print.PrinterException;

/* Imports java.awt package classes. */

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. print() - This method prints the current document opened in the text editor window

Methods:

1 of 4

11/03/2012 6:27

Creating the Print Dialog Box - Java InstantCode: Developing Applicati...

http://book.javanb.com/sillsoft-Press-Java-instantcode-developing-appl...

*/ {

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(); printer.setPrintable(this); if(printer.printDialog()) { try { /* Prints the document */ printer.print();

/* Sets the object of PrinterJob class to printable */

} { }

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 index - Represents an index of page Return Value: int PAGE_EXIST */ {

format - Represents the object of PageFormat class

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

2 of 4

11/03/2012 6:27

Creating the Print Dialog Box - Java InstantCode: Developing Applicati...

http://book.javanb.com/sillsoft-Press-Java-instantcode-developing-appl...

*/ /*

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 */ /* Sets the double buffer to FALSE */ /* Paints the component */ compPrint.paint(g2d); RepaintManager manager1 = RepaintManager.currentManager(compPrint); manager1.setDoubleBufferingEnabled(false);

/* Creates the object of RepaintManager class */ /* Sets the double buffer to TRUE */ /* Returns the PAGE_EXIST value */ } return(PAGE_EXISTS);

RepaintManager manager2 = RepaintManager.currentManager(compPrint); manager2.setDoubleBufferingEnabled(true);

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:

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.

3 of 4

11/03/2012 6:27

Creating the Print Dialog Box - Java InstantCode: Developing Applicati...

http://book.javanb.com/sillsoft-Press-Java-instantcode-developing-appl...

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.

| Sitemap | | | Copyright 2006-2007, Java , All rights reserved

4 of 4

11/03/2012 6:27

Anda mungkin juga menyukai