Java Reference
In-Depth Information
27.
this .setJMenuBar(menuBar);
28.
pack();
29.
}
30.
31.
public void actionPerformed(ActionEvent evt){
32.
String command = evt.getActionCommand();
33.
if (command.equals("Print"))
34.
{
35.
PrinterJob printJob = PrinterJob.getPrinterJob();
36.
printJob.setPrintable(pp);
37.
if (printJob.printDialog()){
38.
try {
39.
printJob.print();
40.
} catch (PrinterException pe) {
System.out.println("Error printing:"+pe);
41.
}
42.
}
43.
}
44.
45.
46.
}
public static void main(String[] args)
47.
{
48.
49.
PrintFrame prfr = new PrintFrame();
50.
prfr.showIt();
51.
}
52.
53.
}
19.4
A generic class for printing
To make a component printable as described above needs the Printable interface
for that component to be implemented. In our example, we derived a PrintPanel
from JPanel . Driving a class from another one just in order to print it is quite a
lot of work. We therefore introduce a generic class PrintSuit which allows you
to print any component without implementing the Printable interface for that
component. In this class we define our own method, called printComponent ,by
public static void printComponent(Component comp)
The argument comp is a graphical component which does not necessarily imple-
ment Printable . This component is printed as a result of this call. Components
embedded into comp are also printed. There is one special case when printing
frames: Only the embedded components of a frame are printed, not the frame it-
!
self. The code of PrintSuit combines the implementation of interface Printable
and the generation of the PrinterJob object. Application PrintSuitTestFrame
Search WWH ::




Custom Search