• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Print a JTextArea

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a problem.
When I want to print a JTextArea with a Vertical Scrollbar, it prints only the first page ! How could I do for printing all ?
The code :
PrintJob pJob = getToolkit().getPrintJob(this,
"Printing_Test", props);
if (pJob != null)
{
Graphics pg = pJob.getGraphics();
myTextArea.printAll(pg);
pg.dispose();
pJob.end();
}
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello friend,
try by using this code......

import java.awt.*;
import javax.swing.*;
import java.awt.print.*;
/** A simple utility class that lets you very simply print
* an arbitrary component. Just pass the component to the
* PrintUtilities.printComponent. The component you want to
* print doesn't need a print method and doesn't have to
* implement any interface or do anything special at all.
*

* If you are going to be printing many times, it is marginally more
* efficient to first do the following:
* <PRE>
* PrintUtilities printHelper = new PrintUtilities(theComponent);
* </PRE>
* then later do printHelper.print(). But this is a very tiny
* difference, so in most cases just do the simpler
* PrintUtilities.printComponent(componentToBePrinted).
*
* 7/99 Marty Hall, http://www.apl.jhu.edu/~hall/java/
* May be freely used or adapted.
*/
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() {
PrinterJob printJob = PrinterJob.getPrinterJob();
Paper p1=new Paper();
p1.setImageableArea(1,1,3,9);
p1.setSize(4,12);
PageFormat pf1=new PageFormat();
pf1.setPaper(p1);
printJob.setPrintable(this,pf1);
//if (printJob.printDialog())
if (true)
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);
}
}
with regards
rajesh
 
Gizzmo Zeuzere
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks but it doesn't work. In fact, it prints only the first page and I don't see how to print the others.
 
hereisrajesh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello friend ,
if u mean to print the content of a whole JTextArea (independent of on or off to the scroll) , surely this code will work.
but the passed componenet should be JTextArea and not be JScrollPane enclosing the JTextArea.I am strong on this because i also once had a similier problem and solved by using the previously mensioned code.
with regards
rajesh
 
hereisrajesh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello friend,
Sorry,
i misunderstood your question,
if you want to print the entire component (with scrollbar) this wont work . The code is for printing the content of a particulr JComponent.
just try once more by passing the JScrollPane instead of JTextArea(but i am not quiet sure).
with regards
rajesh
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gizzmo Zeuzere:
Thanks but it doesn't work. In fact, it prints only the first page and I don't see how to print the others.


What you have to do is calculate the number of pages and check if all the pages have been printed by putting the following if loop in your print method.
if ( pageIndex >= totalNumPages ) {
return Printable.NO_SUCH_PAGE;
}
To calculate the totalNumPages you have to first calculate the page height and the height of the component(e.g panel) you are trying to print.
double pageHeight = pageFormat.getImageableHeight();
double componentHeight = component.getSize().height;
int totalNumPages = (int) Math.ceil(componentHeight/pageHeight);

------------------
 
Gizzmo Zeuzere
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much !
With all these advices, I've succeeded my printing.
Thank ( I've calculated the rows of my JTextArea then, I've calculated the rows per pages. I had so the number of pages.
Then if the page is whole, I print the page with the clipping of my JTextArea, else I recalculate the clipping and I print the new graphics)
Thank you very very much !!!
 
Gizzmo Zeuzere
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//Since, people have asked me the code, I write it
public int print (Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException
{
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);
double pageHeightForTable=oneRowHeight*numRowsOnAPage;
int totalNumPages=(int)Math.ceil(((double)(myTextArea.getRows ()+HEADER))/numRowsOnAPage);
myTextArea.setRows(textWidth);
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("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 clip to the entire area available.
else{
g2.setClip(0,
(int)(pageHeightForTable*pageIndex),
(int) Math.ceil(tableWidthOnPage),
(int) Math.ceil(pageHeightForTable));
}
g2.scale(scale,scale);
myTextArea.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;
}
//==========================================================
// classe interne de gestion d'evts
//actionPerformed of my Button Printing
public void actionPerformed(ActionEvent e)
{
System.out.print(nbLigneJTextArea);
String cmd = e.getActionCommand();
if (cmd.equals("Print"))
{
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(NbreNuitParSemaine3.this);
try{
if(pj.printDialog())
pj.print();
} catch (Exception PrintException) {}
}
}
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hereisrajesh,
Please change your name to be compliant with JavaRanch's naming policy.
Your ID should be 2 separate names with more than 1 letter each. We really want this to be a professional forum and would prefer that you use your REAL name.
Thanks,
Cindy
 
Gizzmo Zeuzere
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And Now the class PrintJText (a meeting of PrintUtilities and my)
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("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;
}
}
Thank for your helps
 
I hired a bunch of ninjas. The fridge is empty, but I can't find them to tell them the mission.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic