-2

I try to create one big String in appropriate format as i want and print it using PrinterJob class. Here is the code of String:

String bigtext = "The name\n" + "some another text";
Graphics2D's_object.drawString(bigtext, 50, 50);

But it prints as "The name some another text" in one line, "\n" does not work, while i want to print "some another text" in another line.

P.S. I try to print bigtext in printer.

SOLVED: Here is the solution: Problems with newline in Graphics2D.drawString. (after long trouble :))

Community
  • 1
  • 1
Nika Tsogiaidze
  • 949
  • 14
  • 18

3 Answers3

1

The linefeed character \n is not the line separator in certain operating systems use \r\n. Additionally i would recommend use of StringBuilder rather then using +

Edit : You can use System.getProperty("line.separator"); as well.

pratim_b
  • 1,160
  • 10
  • 29
1

A similar question was asked yesterday and this is what helped:

The problem that you are encountering is because of the "line separator" you are hard coding. It's best to get the System's line separator with:

System.getProperty("line.separator");

So that your code would look like this:

   String lineseparator=System.getProperty("line.separator");
   // I'd suggest putting this as a class variable, so that it only gets called once
   // rather than everytime you call the addLine() method

  String bigtext = "The name" + lineseparator + "some another text";

 //If you wanted an empty line in between them, then add the lineseparator twice
  String bigtext = "The name" + lineseparator + lineseparator + "some another text";
Community
  • 1
  • 1
Chayemor
  • 3,577
  • 4
  • 31
  • 54
1

It seems most of the guys don't understand the question. I tried the PrinterJob and it doesn't work for me neither.

I found a solution but not verifyied:

How to print strings with line breaks in java

Community
  • 1
  • 1
Xiezi
  • 2,949
  • 3
  • 21
  • 29