Urgent!!!! How do I send the contents of JTextPane to a printer?
1 posts in topic
Flat View  Flat View

Posted By:   Neeliagari_Sreedhar
Posted On:   Monday, October 1, 2001 10:58 PM

How do I print the contents of A JTextPane to a printer?

The contents of a the JTextPane are generated dynamically and could contain images,plain text or other components in it.

Re: Urgent!!!! How do I send the contents of JTextPane to a printer?

Posted By:   hakan_geri  
Posted On:   Tuesday, October 2, 2001 01:24 PM

Hi, i don't remember where but i found a class called PrintUtilities which prints component passed to its print method. I didn't try it for JTextPane but i think it's worth giving a shot. You can simply call the static printComponent( comp ) method. Code is as follows :

import java.awt.*;
import javax.swing.*;
import java.awt.print.*;

public class PrintUtilities implements Printable {
private Component componentToBePrinted;

public static void printComponent(Component c) {
new PrintUtilities(c).print();

public PrintUtilities(Component componentToBePrinted) {
this.componentToBePrinted = componentToBePrinted;

public void print() {
PrinterJob printJob = PrinterJob.getPrinterJob();
if (printJob.printDialog())
try {
} catch(PrinterException pe) {
System.out.println("Error printing: " + pe);

public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
if (pageIndex > 0) {
} else {
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

/** The speed and quality of printing suffers dramatically if
* any of the containers have double buffering turned on.
* So this turns if off globally.
* @see enableDoubleBuffering
public static void disableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);

/** Re-enables double buffering globally. */

public static void enableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);

About | Sitemap | Contact