January 30, 2012

Printing TextPane in java

Java window application to print java textpane



package com.blog.javacode.print;
import javax.swing.JTable;
import javax.swing.JTextPane;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.text.MessageFormat;
public class PrintTextPane {
static MessageFormat headerMessage = new MessageFormat("java project source code");
static MessageFormat footerMessage = new MessageFormat("best java blog");
public static void main(String[] javacode) throws Exception {
PrinterJob printerJob = PrinterJob.getPrinterJob();
if(printerJob.printDialog()) {
JEditorPane jEditorPane = new JEditorPane("text/html", "text");
jEditorPane.read(new BufferedReader(new InputStreamReader(new FileInputStream(new File("belazy.html")))), "");
jEditorPane.repaint();
printerJob.setPrintable(jEditorPane.getPrintable(headerMessage, footerMessage));
printerJob.print();
}}}


The above example prints a java textpane with header and footer. user can define the header and footer here. Please post your valuable comments , after trying this code

Facebook comments