Solved

Printing a JTextArea with text spreading over multiple pages.

Posted on 2002-04-05
7
305 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle SQL syntax check  without executing 6 75
how to install java on RHEL image on EC2 4 41
hibernate example using maven 12 59
Crystal Reports Licensing Questions 4 36
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

830 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