HI
I have a code to print jtable this code is printing like an image......i mean..not printing the headings, and not printing the columns.......
PrintUtilities.printCompon
ent(compon
entToBePri
nted);
import java.awt.*;
import javax.swing.*;
import java.awt.print.*;
public class PrintUtilities implements Printable {
private Component componentToBePrinted;
private String sErr;
public static void printComponent(Component c) {
new PrintUtilities(c).print();
}
public PrintUtilities(Component componentToBePrinted) {
this.componentToBePrinted = componentToBePrinted;
sErr = "";
}
public void print() {
PrinterJob printJob = PrinterJob.getPrinterJob()
;
printJob.setPrintable(this
);
//Display the PrintDialog for printer options
if (printJob.printDialog()){
try {
printJob.print();
} catch(PrinterException pe) {
sErr = "Error printing: " + pe;
}
}
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
Graphics2D g2d = (Graphics2D)g;
disableDoubleBuffering(com
ponentToBe
Printed);
Dimension d = componentToBePrinted.getSi
ze(); //get size of document
double panelWidth = d.width; //width in pixels
double panelHeight = d.height; //height in pixels
double pageHeight = pageFormat.getImageableHei
ght(); //height of printer page
double pageWidth = pageFormat.getImageableWid
th(); //width of printer page
double scale = pageWidth/panelWidth;
int totalNumPages = (int)Math.ceil(scale * panelHeight / pageHeight);
// Make sure not print empty pages
if(pageIndex >= totalNumPages) {
return Printable.NO_SUCH_PAGE;
}
// Shift Graphic to line up with beginning of print-imageable region
g2d.translate(pageFormat.g
etImageabl
eX(), pageFormat.getImageableY()
);
// Shift Graphic to line up with beginning of next page to print
g2d.translate(0f, -pageIndex*pageHeight);
// Scale the page so the width fits...
g2d.scale(scale, scale);
componentToBePrinted.paint
(g2d); //repaint the page for printing
enableDoubleBuffering(comp
onentToBeP
rinted);
return Printable.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.
*/
public static void disableDoubleBuffering(Com
ponent c) {
RepaintManager currentManager = RepaintManager.currentMana
ger(c);
currentManager.setDoubleBu
fferingEna
bled(false
);
}
// Re-enables double buffering globally.
public static void enableDoubleBuffering(Comp
onent c) {
RepaintManager currentManager = RepaintManager.currentMana
ger(c);
currentManager.setDoubleBu
fferingEna
bled(true)
;
}
public String getErrorMsg(){
return sErr;
}
}
Can i use simply jtable.paint(Graphics) in print method.
Bye
Mohan