Printing help please!!! for class assignment

I've looked all over the web and every "tutorial" or page on printing in Java just did not make since like it's a classified secret that nobody wanted to explain. Can someone PLEASE just give me some sample code to print the contents of a TextArea? We are making a Java source code editor for our final project in class and I would like to include a print option. Thanks in advance for any help!
Tim

Comments

  • There has been a discusion on this right here at programmers heaven
    just below.

    look for the heading;
    Printing text from JEditorPane or JTextArea.
    and read there is a code example.

    bashiro

    : I've looked all over the web and every "tutorial" or page on printing in Java just did not make since like it's a classified secret that nobody wanted to explain. Can someone PLEASE just give me some sample code to print the contents of a TextArea? We are making a Java source code editor for our final project in class and I would like to include a print option. Thanks in advance for any help!
    : Tim
    :

  • Printing in java is not quite straight forward. It took me hours of research to figure it out. I can't remember where I got it so I can't give credit to the source.

    I should probably add this to the codepedia section.
    I've only tested it on windows but I imagine that it will work on Linux. You may have to tweak this a little for your project but this should get you started.

    Anyway try this:
    [code]
    import java.awt.print.*;
    import java.awt.geom.*;
    import java.util.*;

    void printButton1_actionPerformed(ActionEvent e) {



    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(new PrintObject());

    if(job.printDialog()){
    try{
    job.print();
    }catch(PrinterException exception){
    }

    }

    }//printButton1
    class PrintObject implements Printable
    {
    public int print (Graphics g, PageFormat f, int pageIndex)
    {
    if (pageIndex==0){


    g.drawRect(80,80,400,160);
    g.drawString("Test: "+ jTextArea1.getText(),100,100);


    return PAGE_EXISTS;
    }
    else return NO_SUCH_PAGE;
    }

    }//PrintObject
    [/code]
    Let me know how it works out.

    Ken
    : There has been a discusion on this right here at programmers heaven
    : just below.
    :
    : look for the heading;
    : Printing text from JEditorPane or JTextArea.
    : and read there is a code example.
    :
    : bashiro
    :
    : : I've looked all over the web and every "tutorial" or page on printing in Java just did not make since like it's a classified secret that nobody wanted to explain. Can someone PLEASE just give me some sample code to print the contents of a TextArea? We are making a Java source code editor for our final project in class and I would like to include a print option. Thanks in advance for any help!
    : : Tim
    : :
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories