Reputation: 281
I am having code of print a string, which is passed in the program itself. Here I am calling this code on Print button to get hard copy. Now I want to print a JForm in the same code, But I am not getting how to do this. JForm having some labels and textfields of user's details. This is the code where I am printing a string"Hello World".
public class PrintClass implements Printable, ActionListener {
public int display(Graphics g, PageFormat pf, int page) throws
PrinterException {
if (page > 0) { /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}
/* User (0,0) is typically outside the imageable area, so we must
* translate by the X and Y values in the PageFormat to avoid clipping
*/
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
/* Now we perform our rendering */
g.drawString("Hello World", 100, 100);
/* tell the caller that this page is part of the printed document */
return PAGE_EXISTS;
}
Please help me to call constructor of a JForm, instead of pass the string.
Upvotes: 1
Views: 681
Reputation: 21409
Draw the Graphics
of your current JFrame
into a BufferedImage
and then draw the image into the printer's Graphics
.
Graphics g = myFrame.getContentPane().getGraphics();
// draw graphics into an image
// draw the image into the printer's graphics
It is important to note that you should always get a new Graphics
object from your JFrame
whenever you want to print the form content
Upvotes: 2