Java Printing

Hi,

can anybody help on the following:

I would like to print (on a printer device) using the Java (Swing) Printing API; I found several examples which all consider to print the content of an external file... I need to print from a Java Object, printing line by line basically. I though I could render the content of the Object by overriding toString(). It doesn't seem to work in this way :(

Any idea, reference, example to submit?

Thanks in advance!

paolo

[494 byte] By [S.O.S.a] at [2007-11-27 5:05:35]
# 1
Which object do you want to print? Can you give us an example?
java_knighta at 2007-7-12 10:24:05 > top of Java-index,Desktop,Core GUI APIs...
# 2

I think you want to print text.

The example below prints three lines of text on a page.

It creates a Book bk.

It calls bk.append(.,.) once, to add one page.

To print more than one page, append more pages.

Pass different Printable's, or pass the same Printable and let it check the pageIndex argument.

import java.awt.Font;

import java.awt.Graphics;

import java.awt.print.Book;

import java.awt.print.PageFormat;

import java.awt.print.Printable;

import java.awt.print.PrinterException;

import java.awt.print.PrinterJob;

public class ReportWriter

{

public static void print()

throws PrinterException

{

print(new PagePainter());

}

private static void print(Printable pagePainter)

throws PrinterException

{

PrinterJob job = PrinterJob.getPrinterJob();

PageFormat pf = job.defaultPage();

Book bk = new Book();

bk.append(pagePainter, job.defaultPage());

job.setPageable(bk);

if (job.printDialog())

job.print();

}

private static class PagePainter implements Printable

{

static final Font fontHeader = new Font("Sans serif", Font.PLAIN, 13);

static final Font fontNormal = new Font("Serif", Font.PLAIN, 11);

static final int spacingHeader = 20;

static final int spacingNormal = 14;

public int print(Graphics g, PageFormat pf, int pageIndex)

throws PrinterException

{

int top = (int) g.getClipBounds().getMinY();

int left = (int) g.getClipBounds().getMinX();

int y = top;

g.setFont(fontHeader);

g.drawString("HEADER", left, y);

y += spacingHeader;

g.setFont(fontNormal);

g.drawString("line1", left, y);

y += spacingNormal;

g.drawString("line2", left, y);

y += spacingNormal;

return Printable.PAGE_EXISTS;

}

}

}

tom_jansena at 2007-7-12 10:24:05 > top of Java-index,Desktop,Core GUI APIs...
# 3
I am writing a simple POS application; I want to print the customer receipt from a Purchase class which is basically an array of (item, quantity, cost) triplets.
S.O.S.a at 2007-7-12 10:24:05 > top of Java-index,Desktop,Core GUI APIs...
# 4
Will check the code and let you know.Meanwhile, thanks!
S.O.S.a at 2007-7-12 10:24:05 > top of Java-index,Desktop,Core GUI APIs...
# 5
Check out my StandardPrint class http://forum.java.sun.com/thread.jspa?forumID=57&threadID=5122492Message was edited by: tjacobs01
tjacobs01a at 2007-7-12 10:24:05 > top of Java-index,Desktop,Core GUI APIs...
# 6
To tom_jansen:I am using your class ReportWriter, I will create the class I want to print as an implementation of Printable and write the print() method. Thanks.
S.O.S.a at 2007-7-12 10:24:05 > top of Java-index,Desktop,Core GUI APIs...