Printing Swing components using a printer
Hello:
I'm developing a Java GUI application with some Swing components on it, such as JTables, JPanels... JFreeCharts , etc.
I would like, if possible, that when I click a button one of these items are printed (yes, printed in the printer, in paper). Is it possible? Can I print the element I choose and give the apropiate format to it?
Please, help me... I've never done something like this! What methods do I have to use? Could you please write some code of example?
Lots of thanks for your help.
Have a nice day.
[555 byte] By [
pujolsa] at [2007-10-3 4:15:59]

Yes you can, but I've found it rather an uphill struggle. You use java.awt.print
What you need to do is to create a Printable object, which you feed to a PrinterJob. The print system then calls your Printable to print individual pages, passing a Graphics2D object. Your printable either returns a "NO_SUCH_PAGE" at the end of the pages to print, or calls the "print" method of the component or components to be printed.
Be warned, the order in which pages are requested is not strictly 0,1,2 ... The same page may be requested serveral times. Pages can be skipped.
Where it gets messey is getting the objects sized an laid out correctly. I suggest you start by finding a "Print Preview" package (there are a few available, e.g. on SourceForge), otherwise you'll work your way through several acres of forest in wasted paper. Trust me, I know whereof I speak ;-)