Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to print in Java?

Status
Not open for further replies.

thelordoftherings

Programmer
May 16, 2004
616
IL
Hello,

I would like to create a button that by clicking on it will open the print component of the PC, just like when I click File->Print... in a browser or in any other window.
What is the code I need to put at the button click event in order to do that?
 
Hey sedj,

Thank you for the link, I do have few questions though.
These examples show how to print something from an input stream, I simply want to open the default printer object and the user will click the "print" once it is opened. I tried to play with this code a bit but haven't managed to acheive this. Could you please show me a code example?

Thank you in advance,
Roy
 
1) What exactly do you want to print ?

2) If you just want to print to the default service, what is the point of opening the OS gui bit ? You may aswell just print straight out.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I just want to open the print service, like the window that opens when I choose File-> Print... at Internet explorer for example...
 
Well if you want the OS print dialog :

PrinterJob pjob = PrinterJob.getPrinterJob();
pjob.printDialog();

But thats not actually going to do a lot on its own - you have to have a class that implements Printable, and is aware of what it actually needs to print ...

eg :

Code:
	class PrintableClass implements Printable {
		public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
			return 0;
		}
	}

	public void doit() {
		PrinterJob pjob = PrinterJob.getPrinterJob();
		pjob.printDialog();
		PageFormat pf = pjob.defaultPage();
		pjob.setPrintable(new PrintableClass(), pf);
		try {
			if (pjob.printDialog()) {
				pjob.print();
			}
		} catch (PrinterException e) {
			e.printStackTrace(System.out);
		}
	}

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top