CONTENTS | PREV | NEXT | Java 2D API |
Typically, you will want to display a print dialog to the user before initiating the print job. The print dialog enables the user to specify the pages and number of copies to be printed. To display this print dialog, you call PrintJob.setupJob. This method returns TRUE when the user clicks the OK button in the print dialog.
import Java.lang.*;
import Java.awt.*;
import Java.awt.print.*;
public class PrintWithDialog {
public static void main(String[] args) {
/* Create a new book with two pages all using
* the same PageFormat and Printable instance.
*/
Book book = new Book();
book.append(new PageFormat(), new NumberPainter(), 2);
/* Get a print job from the graphics environment and
* tell it to print our book of two pages.
*/
PrintJob job = GraphicsEnvironment.getLocalGraphicsEnvironment().getPrintJob();
/* Display the print dialog & print the book if
* setupJob returns TRUE.
*/
if(job.setupJob(book) == true){
job.print(book);
}
System.exit(0);
}
}