wood burning stoves 2.0*
The moose likes Java in General and the fly likes printing - can't have a '\n' new line. everything is concatenated :-( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "printing - can Watch "printing - can New topic
Author

printing - can't have a '\n' new line. everything is concatenated :-(

Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
Hi guys,

can anyone tell me why th code below doesn't print a new line??? (everthing is concatenated)



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.print.*;

public class Printer extends JFrame
{

private JTextArea t1;

public Printer()
{
super("Printer . . .");
t1 = new JTextArea();
Container c = getContentPane();
JPanel p1 = new JPanel();
JButton b1 = new JButton("Print Me");
p1.add(b1, BorderLayout.CENTER);
c.add(t1, BorderLayout.CENTER);
c.add(p1, BorderLayout.SOUTH);
Dimension sd = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(sd.width / 2 - 400 / 2,
sd.height / 2 - 350 / 2);
setResizable(false);
setSize(400, 350);
show();
b1.addActionListener(new ActionListener()
{

public void actionPerformed(ActionEvent e)
{
printPage(t1.getText());
}

public void printPage(String textToPrint)
{
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat landscape = job.defaultPage();
landscape.setOrientation(PageFormat.LANDSCAPE);
Book bk = new Book();
bk.append(new PaintCover(), job.defaultPage());
job.setPageable(bk);
if (job.printDialog())
{
try {
job.print();
}catch (Exception exc)
{
System.out.println(exc);
}
}
}


class PaintCover implements Printable
{
Font fnt = new Font("Helvetica-Bold", Font.PLAIN, 48);

public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException
{
g.setFont(fnt);
g.setColor(Color.black);
g.drawString(t1.getText(), 100, 200);
return Printable.PAGE_EXISTS;
}
}

}
);
}


// Main method

public static void main( String args[] )
{
Printer app = new Printer();

app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
Jimmy Die
Ranch Hand

Joined: Nov 20, 2003
Posts: 97
Hi,


Try
t1.setLineWrap(true);


Jimmy Die
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
thanks, but it doesn't work

any other idea?
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
It looks to me like your problem is with this line:


What you may want to try, is to get the text (like you have above) and tokenize it using the new line character as your token. Then enter into a loop that prints the different tokens that you parse out. Just don't forget to adjust your coordinates as you loop through the tokens.
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
perfect, StingTokenizer solves it!!!

thanks,

how about indentation, any idea?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I don't know if this is the best way, but can't you just adjust the x coordinate that you send to drawString()?

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: printing - can't have a '\n' new line. everything is concatenated :-(