JTable printing problem

hello, i m using the following code to print the JTable.

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() {

PageFormat pageFormat=new PageFormat();

Paper paper = pageFormat.getPaper();

paper.setImageableArea(0, 0.5D*72, 8.267D*72, 10.692D*72);

paper.setSize(8.267D*72, 11.692D*72);

pageFormat.setPaper(paper);

PrinterJob printJob = PrinterJob.getPrinterJob();

printJob.setPrintable(this);

if (printJob.printDialog())

try {

printJob.print();

} catch(PrinterException pe) {

System.out.println("Error printing: " + pe);

}

}

public int print(Graphics g, PageFormat pageFormat, int pageIndex) {

if (pageIndex > 0) {

return(NO_SUCH_PAGE);

} else {

Graphics2D g2d = (Graphics2D)g;

g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

disableDoubleBuffering(componentToBePrinted);

componentToBePrinted.paint(g2d);

enableDoubleBuffering(componentToBePrinted);

return(PAGE_EXISTS);

}

}

/** 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);

currentManager.setDoubleBufferingEnabled(false);

}

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

public static void enableDoubleBuffering(Component c) {

RepaintManager currentManager = RepaintManager.currentManager(c);

currentManager.setDoubleBufferingEnabled(true);

}

}

//******************************//

PrintUtilities.printComponent(table);

Its printing the data in the JTable properly n correctly, but its not printing the column names or the column heads in the print-out.

Plz help me.

Thanks in advance.

[2385 byte] By [mansi_b85a] at [2007-11-27 1:30:28]
# 1
Are you aware that is the current version of the J2SE (6) or the previous version (5),you can print a table as simply as this:table.print(); //done!
DrLaszloJamfa at 2007-7-12 0:31:45 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks for reply.I m currently using j2sdk1.4.0, but will surely try it using jdk1.5but does it print the column header also?
mansi_b85a at 2007-7-12 0:31:45 > top of Java-index,Java Essentials,Java Programming...
# 3

> Thanks for reply.

> I m currently using j2sdk1.4.0, but will surely try

> it using jdk1.5

> but does it print the column header also?

Yes. There are additional print methods that allow you to supply page headers, footers, etc:

http://java.sun.com/javase/6/docs/api/javax/swing/JTable.html

DrLaszloJamfa at 2007-7-12 0:31:45 > top of Java-index,Java Essentials,Java Programming...
# 4
tried table.print() in jdk1.5, but giving an error : unreported exception java.awt.print.PrinterException; must becaught or declared to be thrown table.print();plz help
mansi_b85a at 2007-7-12 0:31:45 > top of Java-index,Java Essentials,Java Programming...
# 5
http://java.sun.com/docs/books/tutorial/essential/exceptions/index.htmlIf you don't know how to work with exceptions, you should stop writingSwing code, and brush up on language fundamentals.
DrLaszloJamfa at 2007-7-12 0:31:45 > top of Java-index,Java Essentials,Java Programming...
# 6
sorry i m new to java and as per my syllabus we are suppose to do huge amount of work in less time. so no time to study all things.but thanks once again for help.
mansi_b85a at 2007-7-12 0:31:45 > top of Java-index,Java Essentials,Java Programming...
# 7

Then my criticism is directed to whoever designed your course.

Poorly designed courses tend to push students into "gee-whiz"

area of the API like Swing before the students are ready for it,

often skipping over key areas that are actually more important,

like exception handling.

DrLaszloJamfa at 2007-7-12 0:31:45 > top of Java-index,Java Essentials,Java Programming...
# 8
Thanks to tell me to use jdk1.5MY printing problem has solved and also it gives a new and shining look out to my project.Thanks once more.
mansi_b85a at 2007-7-12 0:31:45 > top of Java-index,Java Essentials,Java Programming...
# 9
Good luck, and keep catching those exceptions!
DrLaszloJamfa at 2007-7-12 0:31:45 > top of Java-index,Java Essentials,Java Programming...