JTextArea Printing

2.5k views Asked by At

I have an JTextArea with some lines of text in it.I want to print that lines of text, here i am using the method getText() and storing the whole data into a string variable. I am passing that string to the print class. while printing that string, text is printed without any spaces, new line or tabs etc. can any one help me by solving my problem.

My printing code

public int print(Graphics g, PageFormat pf, int page) throws PrinterException 
    {


        if (page > 0) 
        {                                                                                           
            return NO_SUCH_PAGE;
        }
        Graphics2D g2d = (Graphics2D)g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());
        g.drawString(data,10,10);
        return PAGE_EXISTS;
    }

Here data is my string variable.

thank u...

2

There are 2 answers

0
StanislavL On

I would call it this way

Graphics2D g2d = (Graphics2D)g;         
g2d.translate(pf.getImageableX(), pf.getImageableY());         
myTextArea.paint(g); 
0
jzd On

If your data variable has spaces it should be included when you draw the string. However drawString does not handle new lines for you.

See this question about how to handle this: How to output a String on multiple lines using Graphics