0

I want to take the print out of jFrame ,I can use printutility and all necessory for printing. Now my problem is that how to send the jFrame to printer for print out of jFrame?

Is it possible to take the print out of jFrame or any other solution?

Luna
  • 956
  • 4
  • 15
  • 28
Jayashri
  • 366
  • 4
  • 13
  • 25

1 Answers1

0

You should take an image of the JFrame by painting the component to an image, and then send the image to a printer using the java.awt.print API.

public static BufferedImage takeScreenshot(Component comp) {
    BufferedImage image = new BufferedImage(comp.getWidth(), comp.getHeight(), BufferedImage.TYPE_INT_RGB);
    component.paint(image.getGraphics());
    return image;
}
FThompson
  • 28,352
  • 13
  • 60
  • 93
  • I have a PrintUitility class which contains the method – Jayashri Jun 11 '12 at 05:40
  • Contains a method to print an image? If so, then you are ready to print, correct? – FThompson Jun 11 '12 at 05:42
  • public int print(Graphics g, PageFormat pageFormat, int pageIndex) Can I write ur solution in this method – Jayashri Jun 11 '12 at 05:45
  • Or, create a new Graphics object and simply paint the component to it and then pass that to the print method. – FThompson Jun 11 '12 at 05:46
  • yes Im ready to print but I dont know how to send the Jframe to printer in print() method and im using netbeans – Jayashri Jun 11 '12 at 05:47
  • my JFrame class is GatepassVisitor() I want to print the This JFrame ... and I have a PrintUtility() class which contains print() method now I have to pass the object of GatepassVisitor() to the print method is this right – Jayashri Jun 11 '12 at 05:54
  • Pass your GatepassVisitor instance to the takeScreenshot method I posted, and then print the returned image. – FThompson Jun 11 '12 at 07:18