CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2001
    Location
    Puerto Rico
    Posts
    26

    I want to implement a print button that prints a textarea...

    Hi everyone!
    I need to implement a Print button to print the text contained on a JtextArea, does anyone can help me do it? I've used the following code, but it doesn,t work:

    else if(command == "Print Code...")
    {
    PrinterJob printJob = PrinterJob.getPrinterJob();
    String text = codeTextArea.getText();
    printJob.setPrintable(text);
    if (printJob.printDialog()) {
    }
    }




    the compiler gives me the following error on it
    JavaFFT.java:349: cannot resolve symbol
    symbol : method setPrintable (java.lang.String)
    location: class java.awt.print.PrinterJob
    printJob.setPrintable(text);
    ^
    1 error

    Also, I dont know how to specify that I want to print the JTextArea named codeTextArea...

    Please someone help me as soon as possible!!!
    Thanx



    JNP

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: I want to implement a print button that prints a textarea...

    If you read the JavaDocs for PrinterJob (http://java.sun.com/j2se/1.3/docs/ap...rinterJob.html), you'll find that setPrintable(...) takes a Printable object, not a String. It does help to read the docs occasionally...

    A simple way to print the contents of any Component (e.g. a JTextArea) might look like this: PrintJob pjob = getToolkit().getPrintJob(this,
    "Print TextArea", null);

    if (pjob != null) {
    Graphics pg = pjob.getGraphics();

    if (pg != null) {
    myTextArea.printAll(pg); // Print the Component contents
    pg.dispose(); // flush page
    }
    pjob.end();
    }

    The Java Tutorial has an example of printing component contents by implementing Printable: http://java.sun.com/docs/books/tutor...component.html,

    You might find these articles useful:
    http://java.sun.com/docs/books/tutor.../overview.html
    http://manning.spindoczine.com/sbe/f.../Chapter22.htm

    Dave

    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: I want to implement a print button that prints a textarea...

    Incidentally, the easiest way I know to do what you want is to download the PrintUtilities class from http://www.apl.jhu.edu/~hall/java/Sw...-Printing.html. Then you can just do:PrintUtilities.printComponent(textField);

    Which saves a lot of hassle ;-)

    Dave

    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: I want to implement a print button that prints a textarea...

    A comment about your code:
    The if test: command == "Print Code..."
    is NOT a good way to see it a String variable has a certain value. The == operator tests for equality of reference variables or primitives, not equality of the contents of the objects referenced. Two reference variables(pointers) have the same value if they both refer to the same object.
    You should use the class's equals() method (or what ever the class's programmer has provided) to test for equality.



    Norm
    Norm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured