upendra
upendra

Reputation: 1

JTextArea Printing

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...

Upvotes: 0

Views: 2505

Answers (2)

StanislavL
StanislavL

Reputation: 57381

I would call it this way

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

Upvotes: 0

jzd
jzd

Reputation: 23629

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

Upvotes: 1

Related Questions