[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Printing a JTextArea with text spreading over multiple pages.

Posted on 2002-04-05
7
Medium Priority
?
326 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 600 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

831 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