Good solution to print JTable is using RReport (www.java4less.com)
Marcin Zawadzki
Main Topics
Browse All TopicsHI
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
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
Dimension d = componentToBePrinted.getSi
double panelWidth = d.width; //width in pixels
double panelHeight = d.height; //height in pixels
double pageHeight = pageFormat.getImageableHei
double pageWidth = pageFormat.getImageableWid
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
// 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
enableDoubleBuffering(comp
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
RepaintManager currentManager = RepaintManager.currentMana
currentManager.setDoubleBu
}
// Re-enables double buffering globally.
public static void enableDoubleBuffering(Comp
RepaintManager currentManager = RepaintManager.currentMana
currentManager.setDoubleBu
}
public String getErrorMsg(){
return sErr;
}
}
Can i use simply jtable.paint(Graphics) in print method.
Bye
Mohan
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Good solution to print JTable is using RReport (www.java4less.com)
Marcin Zawadzki
Marcin, I see that you're new so I'll let you in on a little etiquette here. Don't post a 'proposed answer' unless it completely answers the asked question exactly, and is the only possible solution. Once you do, the question is locked and moved to a different area, and most experts won't comment on them. Also, since the asker can accept any comment made as an answer, there is no reason to lock the question at all.
Cheers,
Kylar
Here's some code that works.
For you second question, yes you can use paint() instead of print() but paint() is the "old" way whereas print() is the "new" prefered way since it also takes care of turning off double-buffering during printing to speed things up.
Here's the code, use it wisely ;) You need to have a reference to the JTable you want printed though ... so that class that will hold this print() method needs a reference to the JTable. In the code below that reference is simply called "table".
public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
Graphics2D g2 = (Graphics2D) g;
double scale = 1;
//calculate the scaling factor (if needed)
double pageHeight = pf.getImageableHeight();
double pageWidth = pf.getImageableWidth();
double tableWidth = (double)table.getColumnMod
if (tableWidth >= pageWidth) { scale = pageWidth / tableWidth; }
double headerHeightOnPage = table.getTableHeader().get
double tableWidthOnPage = tableWidth * scale;
double oneRowHeight = table.getRowHeight() * scale;
int numRowsOnAPage = (int)Math.floor((pageHeigh
double pageHeightForTable = oneRowHeight * numRowsOnAPage;
int totalNumPages = (int)Math.ceil(((double)ta
if(pageIndex >= totalNumPages) { return NO_SUCH_PAGE; }
//move the pen to 0,0 of the page (takes into account margins)
g2.translate(pf.getImageab
if (pageIndex == 0) {
g2.translate(0f, 0f);
g2.scale(scale,scale);
table.getTableHeader().pri
g2.scale(1/scale, 1/scale);
g2.translate(0f, headerHeightOnPage);
g2.setClip(0, 0, (int)Math.ceil(tableWidthO
g2.scale(scale,scale);
table.print(g2);
g2.scale(1/scale, 1/scale);
}
else {
g2.scale(scale,scale);
table.getTableHeader().pri
g2.scale(1/scale, 1/scale);
//needed to place pen below table header
g2.translate(0f, headerHeightOnPage);
g2.translate(0f, -(pageHeightForTable * (pageIndex-1) + pageHeightForTableOnFirstP
g2.setClip(0, (int)Math.ceil(pageHeightF
g2.scale(scale,scale);
table.print(g2);
g2.scale(1/scale, 1/scale);
}
return Printable.PAGE_EXISTS;
}
Dear Marcin_Zawadzki
I've rejected your proposed answer as Experts Exchange holds an experiment to work without the answer button.
See: http://www.experts-exchang
Paragraph: Site Update for Wednesday, November 06, 2002
By this rejection the Asker will be notified by mail and hopefully he will take his responsibility to finalize the question or post an additional comment.
The Asker sees a button beside every post which says "Accept This Comment As Answer" (including rejected answers) -- so if he/she thinks yours is the best, you'll be awarded the points and the grade.
This Q hasn't been active lately, thus I've posted it in the CS Cleanup topic area.
I'll return to this Q in seven days to see if any coment has been added, if not then it's my intention that this question is:
-Answered by: totsubo
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !
Thanks !
DigitalXtreme
Community Support Moderator
Experts Exchange
mohangl,
have a look at this.
http://developer.java.sun.
Hope this will help you.
Good luck !
Mirinda
Business Accounts
Answer for Membership
by: kylarPosted on 2002-09-03 at 12:24:05ID: 7259659
PAQ's that show how to print a JTable:
e.com/Prog ramming/ Pr ogramming_ Languages/ Java/Q_201 36146.html
e.com/Prog ramming/ Pr ogramming_ Languages/ Java/Q_201 23708.html
http://www.experts-exchang
Generic Printing(JEditorPane)
http://www.experts-exchang
Cheers,
Kylar