Hello,
I have a question about printing components in Java, and in particular
printing a JTextArea component. In the example below I am simply trying
to print a single JTextArea component. As you will notice I am sizing
this component to the imageable width and height of the PaperFormat
object and invoking the print(graphics) method. However instead of
painting to the desired width the JTextComponent prints off the
imageable area of the paper and in doing so does not print the "i"'s
between 13-16 on a standard 8x11 portrait size paper.
Does anyone have any ideas what is going on here? Thanks in advance.
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Pageable;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JTextArea;
import javax.swing.RepaintManager;
public class PrintTest {
public static void main(String[] args) {
PrintClass pc = new PrintClass();
pc.print();
}
public static class PrintClass extends Object implements Printable,
Pageable {
private PrinterJob printerJob;
private PageFormat pageFormat;
private JTextArea textPane;
/**
* Default constructor.
*/
public PrintClass() {
super();
textPane = new JTextArea();
textPane.setText("iiiiiiiiii1iiiiiiiiii2iiiiiiiiii3iiiiiiiiii4iiiiiiiiii5iiiiiiiiii6iiiiiiiiii7iiiiiiiiii8iiiiiiiiii9iiiiiiiiii10iiiiiiiiii11iiiiiiiiii12iiiiiiiiii13iiiiiiiiii14iiiiiiiiii15iiiiiiiiii16iiiiiiiiii17iiiiiiiiii18iiiiiiiiii19iiiiiiiiii20");
textPane.setFont(new Font("Tahoma", Font.PLAIN, 12));
textPane.setLineWrap(true);
textPane.setWrapStyleWord(true);
}
// Implements Pageable.getNumberOfPages
public int getNumberOfPages() {
return 1;
}
// Implements Pageable.getPageFormat
public PageFormat getPageFormat(int pageIndex) throws
IndexOutOfBoundsException {
return pageFormat;
}
// Implements Pageable.getPrintable
public Printable getPrintable(int pageIndex) throws
IndexOutOfBoundsException {
return this;
}
// Implements Printable.print(Graphics, PageFormat, int)
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
final Graphics2D g2 = (Graphics2D)g;
RepaintManager currentManager =
RepaintManager.currentManager(textPane);
currentManager.setDoubleBufferingEnabled(false);
g2.translate(pageFormat.getImageableX(),
pageFormat.getImageableY());
textPane.setSize((int)pageFormat.getImageableWidth(),
(int)pageFormat.getImageableHeight());
textPane.print(g);
currentManager.setDoubleBufferingEnabled(true);
return Printable.PAGE_EXISTS;
}
/**
* Prints the contents of the window.
*/
public void print() {
printerJob = PrinterJob.getPrinterJob();
pageFormat = printerJob.defaultPage();
printerJob.setPageable(this);
try {
printerJob.print();
} catch (PrinterException ignored) {
}
}
}
}
Bill Tschumy - 23 Jun 2005 20:17 GMT
> Hello,
>
[quoted text clipped - 8 lines]
>
> Does anyone have any ideas what is going on here? Thanks in advance.
<code snipped>
This is just a guess, but you are resizing the JTextPane in the paint()
method. I would try doing it somewhat earlier in the print process.
If you don't want to change where the resize happens you will probably need
to reset the clip rect to the new size in paint(). You may be not seeing all
the printing because it is clipped.

Signature
Bill Tschumy
Otherwise -- Austin, TX
http://www.otherwise.com
Mr. Smith - 23 Jun 2005 20:54 GMT
Thanks for the response Bill. I tried moving the setSize() call outside
the print() method but that had no effect. For some reason (I am sure a
simple one) the JTextPane still paints about 88 pixels wider than the
imageable area.