• Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing Printing problem  RSS feed

 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I got code for printing from JTextArea from this forum. I tried to run it, but all I get is the page number and no output from the JTextArea.
Here is the code:
import java.awt.*;
import javax.swing.*;
import java.awt.print.*;
public class PrintJText implements Printable {
private JTextArea maJTextArea;
public static void printComponent(JTextArea maJTextArea) {
new PrintJText(maJTextArea).print();
}
public PrintJText(JTextArea maJTextArea) {
this.maJTextArea = maJTextArea;
}
public void print() {
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(PrintJText.this);
try{
if (pj.printDialog())
pj.print();
}
catch (Exception PrintException) {}
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.black);
int fontHeight = g2.getFontMetrics().getHeight();
int fontDesent = g2.getFontMetrics().getDescent();
double pageHeight = pageFormat.getImageableHeight() - fontHeight;
double pageWidth = pageFormat.getImageableWidth();
double headerHeightOnPage =16.5;
double oneRowHeight = fontHeight;
int numRowsOnAPage = (int) ((pageHeight - headerHeightOnPage)/oneRowHeight);
int totalNumPages=(int)Math.ceil(((double)(maJTextArea.getLineCount())/numRowsOnAPage));
System.out.println(totalNumPages);
double pageHeightForTable=oneRowHeight*numRowsOnAPage;
double tableWidth = 468;
double scale=1;
if (tableWidth >= pageWidth) {
scale = pageWidth / tableWidth;
}
double tableWidthOnPage = tableWidth*scale;
if(pageIndex>=totalNumPages) {
return NO_SUCH_PAGE;
}
g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
g2.drawString("Printing Page: "+(pageIndex+1), (int)pageWidth/2-35, (int)(pageHeight +fontHeight-fontDesent));
g2.translate(0f,headerHeightOnPage);
g2.translate(0f,-pageIndex*pageHeightForTable);
if (pageIndex + 1 == totalNumPages) {
int lastRowPrinted = numRowsOnAPage * pageIndex;
int numRowsLeft = totalNumPages*numRowsOnAPage - lastRowPrinted;
g2.setClip(0,(int)(pageHeightForTable * pageIndex),(int) Math.ceil(tableWidthOnPage),(int) Math.ceil(oneRowHeight * numRowsLeft));
}
else{
g2.setClip(0,(int)(pageHeightForTable*pageIndex),(int) Math.ceil(tableWidthOnPage),(int) Math.ceil(pageHeightForTable));
}
g2.scale(scale,scale);
maJTextArea.paint(g2);
g2.scale(1/scale,1/scale);
g2.translate(0f,pageIndex*pageHeightForTable);
g2.translate(0f, -headerHeightOnPage);
g2.setClip(0, 0,
(int) Math.ceil(tableWidthOnPage),
(int)Math.ceil(headerHeightOnPage));
g2.scale(scale,scale);
return Printable.PAGE_EXISTS;
}
public static void main(String[] args){
System.out.println("trying to print...");
PrintJText.printComponent(new JTextArea("test111"));
}
}
Can any one advise?
thanks,
Alex
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!