wood burning stoves 2.0*
The moose likes Java in General and the fly likes Printing in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Printing in Java " Watch "Printing in Java " New topic
Author

Printing in Java

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Hi all ,

Can we add the print functionality in a JButton that print the content of the JFrame .If any body know then please mail me with some code .

Actully what my project need is I have to take printout of every customer datas . I am able to retrive all the datas of a particular customer on a JFrame & now I have to take printout of this . Please put ur reply fast .

Thanks ,
Ankur
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Yes you can print with Java. No, I won't write the code for you - but I will point you at this where you can learn how to yourself.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
My question was different .
please any body put attention on that .

thanks,
ankur
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


My question was different

How so?
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
I want to print the contents of a JFrame .
Please help me out .
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Please help me ...
Its very necessary ...
Please ...
Sebastiaan Kortleven
Ranch Hand

Joined: Mar 12, 2004
Posts: 81
There's no such method as myFrame.print(); You'll have to write the code yourself.. it's not that hard.. check out the printing API...

The code looks a lot like the code in a paint method..
Sainath Veepuri
Ranch Hand

Joined: Sep 25, 2003
Posts: 49
Hi Rathi ji,

The code below would print all the (contents) components on the JFrame.
Please be aware that this code is not tested. I'am just trying to help you out.
===================================================
// jFrame is the frame which you want to print.
Component[] components = jFrame.getComponents();
for (int i = 0; i < components.length; i++) {
// prints the contents of the frame.
System.out.println(components[i]);
}
===================================================
Does this fit your need.
- Sai
Ashish Vegaraju
Ranch Hand

Joined: Aug 19, 2004
Posts: 47
Hi Rathi,

u can log on to this siteactivetree. here u can find smartJPrint APIs, which can be used to print swing components.

No, Sainath Veepuri, without even testing i can say that...the components will not be printed, using that code.

cheers.
Ashish
[ December 06, 2004: Message edited by: Ashish Vegaraju ]
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
HI Sainath ,

Thanks for help but my problem is different .
Suppose a JFrame has some data & I want to give a JButton for printing . Whenever I click that button , printout should come with whatever on JFrame .

Please any body help .
Thanks
Sainath Veepuri
Ranch Hand

Joined: Sep 25, 2003
Posts: 49
Hi Rathi ji,

I'am just trying to do which is being discussed here.
please check if this code would be of any use to you.
- SAi

==================================================
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;

/*
* Created on Dec 7, 2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @author sainathv
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ScreenPrint {

private JFrame jFrame = new JFrame();

private BufferedImage captureCurrentFrame(){
try {
Robot robot = new Robot();
// Capture a particular area on the screen
int x = getX();
int y = getY();
int width = getWidth();
int height = getHeight();
Rectangle area = new Rectangle(x, y, width, height);
BufferedImage bufferedImage = robot.createScreenCapture(area);
bufferedImage = robot.createScreenCapture(area);
return bufferedImage;
} catch (AWTException e) {
e.printStackTrace();
return null;
}
}

private void showDummyFrame(){
jFrame.getContentPane().add(new JLabel("TEST TEST TEST"));
jFrame.setSize(200, 200);
jFrame.setLocation(450, 450);
jFrame.setTitle("Here comes the title");
jFrame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.out.println("CLOSING");
print();
}});
jFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
jFrame.setVisible(true);
}

private int getX(){
return jFrame.getX();
}
private int getY(){
return jFrame.getY();
}
private int getWidth(){
return jFrame.getWidth();
}
private int getHeight(){
return jFrame.getHeight();
}


public void print() {
try {

// Open the image file
InputStream is = new BufferedInputStream(
new FileInputStream(createFile(captureCurrentFrame())));
// Find the default service
DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;

PrintService service = PrintServiceLookup.lookupDefaultPrintService();

// Create the print job
DocPrintJob job = service.createPrintJob();

System.out.println("service----> "+service);

Doc doc = new SimpleDoc(is, flavor, null);

// Monitor print job events;
PrintJobWatcher pjDone = new PrintJobWatcher(job);
// Print it
job.print(doc, null);
// Wait for the print job to be done
pjDone.waitForDone();
System.out.println("FINISHED..............");

} catch (PrintException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private File createFile(BufferedImage bImage){
// Write generated image to a file
try {
// Save as jpeg
File file = File.createTempFile("temp", ".jpeg");
ImageIO.write(bImage, "jpeg", file);
return file;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

public static void main(String args[]){
ScreenPrint sPrint = new ScreenPrint();
sPrint.showDummyFrame();
}
}

class PrintJobWatcher {
// true if it is safe to close the print job's input stream
boolean done = false;

PrintJobWatcher(DocPrintJob job) {
// Add a listener to the print job
job.addPrintJobListener(new PrintJobAdapter() {
public void printJobCanceled(PrintJobEvent pje) {
System.out.println("printJobCanceled");
allDone();
}
public void printJobCompleted(PrintJobEvent pje) {
System.out.println("printJobCompleted");
allDone();
}
public void printJobFailed(PrintJobEvent pje) {
System.out.println("printJobFailed");
allDone();
}
public void printJobNoMoreEvents(PrintJobEvent pje) {
System.out.println("printJobNoMoreEvents");
allDone();
}
void allDone() {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
});
}

public synchronized void waitForDone() {
try {
while (!done)
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
[ December 07, 2004: Message edited by: Sainath Veepuri ]
 
Don't get me started about those stupid light bulbs.
 
subject: Printing in Java