• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 );
}
}
);
}
}
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


Try
t1.setLineWrap(true);
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks, but it doesn't work

any other idea?
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
perfect, StingTokenizer solves it!!!

thanks,

how about indentation, any idea?
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic