Solved

Printing a JTextArea with text spreading over multiple pages.

Posted on 2002-04-05
7
277 Views
Last Modified: 2010-03-31
Hi,
I am trying to print the text in JTextArea. The printing is done through a class 'PrintUtilities' which can print all 'Component's. But, when tried to print with text that spreads over multiple pages (say 2 pages), I am getting 2 copies of first 41 lines. The text starting from 42 line onwards (2nd page) are not getting printed. Instead I am getting two copies of first 41 lines. i.e in the
following code jTextArea1 contains 80 lines of text. When it got printed I am getting 2 copies of first 41 lines.
Thanking in advance,
Regards,
Unnic.
---- <code>-----

// the PrintUtilities which will print a Component...

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();
    printJob.setPrintable(this);
    try {
        printJob.print();
      } catch(PrinterException pe) {
      }
  }

  public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
// calculating the no. of pages required...
     int width =     this.component.getHeight();
     double imgHeight = pf.getImageableHeight();
     int numpages=  0;
     numpages = (int)(width/imgHeight);
     if(numpages*imgHeight < width)
       numpages++;
    if (pageIndex > numpages) {
      return(NO_SUCH_PAGE);
    } else {
      Graphics2D g2d = (Graphics2D)g;
      g2d.translate(pageFormat.getImageableX(),    
      pageFormat.getImageableY());
      componentToBePrinted.paint(g2d);
      return(PAGE_EXISTS);
    }
  }
// Invoking the print
    PrintUtilities pu = new PrintUtilities(this.jTextArea1);
    pu.print();
---- <code>-----

0
Comment
Question by:unnic
  • 3
  • 3
7 Comments
 
LVL 92

Accepted Solution

by:
objects earned 150 total points
ID: 6922480
You do not appear to be taking notice of the requested pageIndex passed to print().
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7933214
This question has been abandoned. I will make a recommendation to the moderators on its resolution in a week or two. I appreciate any comments that would help me to make a recommendation.
 
In the absence of responses, I may recommend DELETE unless it is clear to me that it has value as a PAQ. Silence = you don't care
 
Venci75
EE Cleanup Volunteer
0
 
LVL 92

Expert Comment

by:objects
ID: 7936084
I believe I answered the question.
The page required to be printed is passed to the print method, but the above code ignores it. Thus it prints the 1st page on every page.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 9

Expert Comment

by:Venci75
ID: 7940874
ok - it seems that unnic does not participate in his own questions. objects - I think you are right - but let me give few days to unnic. If he doesn't respond the next 3 days - I will recomend your comment as an answer.
ok?
0
 
LVL 92

Expert Comment

by:objects
ID: 7946070
sounds fine.
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7973702
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by: objects
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venci75
EE Cleanup Volunteer
0
 

Expert Comment

by:SpideyMod
ID: 8020296
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now