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_EX开发者_StackOverflow社区ISTS;
}
Here data is my string variable.
thank u...
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
I would call it this way
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
myTextArea.paint(g);
精彩评论