wood burning stoves 2.0*
The moose likes Java in General and the fly likes help with prints Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "help with prints" Watch "help with prints" New topic
Author

help with prints

khella smith
Ranch Hand

Joined: May 06, 2002
Posts: 39
hi all,
i wrote this program using the printing module and i read through some examples at the suns tutorial and also at javabotique.i programmed a gui with a text field and a button which will print when the button is clicked.when u click at the button it the programe communicates with the printer but then the text in the textfield is not printed out it prints only blank.could anyone help?
thanks
i decided to give u all the code.some changes in it might have been due to too much messing around with it
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.print.*;
import java.awt.font.*;
public class Print extends JFrame implements ActionListener,Printable{
String c;
JLabel label = new JLabel("Text");
JPanel pan = new JPanel();
JPanel pan1 = new JPanel();
JPanel panel = new JPanel();
Font font= new Font("serif", Font.BOLD,10);
JTextField field = new JTextField(5);
JButton butt = new JButton("Clear");
JButton butt2 = new JButton("Print");
JTextField field2 = new JTextField(5);
Print(){
super("testing print");
setSize(400,200);
setLocation(200,100);
Container pane = getContentPane();
butt.addActionListener(this);
butt2.addActionListener(this);
BorderLayout bord = new BorderLayout();
GridLayout grid = new GridLayout(2,1);
FlowLayout flow = new FlowLayout();
FlowLayout flow2 = new FlowLayout();
pan.setLayout(flow);
pan1.setLayout(flow2);
pane.setLayout(grid);
pan.add(label);
field.setFont(font);
field2.setFont(font);
pan.add(field);
pan.add(field2);
pan1.add(butt);
pan1.add(butt2);
pane.add(pan);
pane.add(pan1);
setContentPane(pane);
setVisible(true);
}
public static void main(String [] args){
Print p = new Print();}
public void actionPerformed(ActionEvent e){
Object command = e.getSource();
if(command ==butt){
String a = field.getText();
field.setText(null);
field2.setText(a);
}
if(command == butt2){
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
c= field.getText();
//printJob.getText();
if(printJob.printDialog()){
try{
printJob.print();
}
catch(Exception ea){
ea.printStackTrace();}
}
}
}
public int print(Graphics g,PageFormat pf, int pi) throws PrinterException{
if(pi >= 1){
return Printable.NO_SUCH_PAGE;}
Graphics2D g2d = (Graphics2D) g;
//String b = "say cheese";
FontMetrics fontmetrics = g2d.getFontMetrics();
double titlex = (pf.getImageableWidth()/2) - (fontmetrics.stringWidth(c)/2);
double titley = 1;

g2d.drawString(c,(int)titlex,(int)titley);
return Printable.PAGE_EXISTS;
}}
[ June 27, 2002: Message edited by: khella smith ]
Rene Liebmann
Ranch Hand

Joined: May 02, 2002
Posts: 196
Ok,
printing text is not that easy.
First, you need to break lines, when your printing space is over.
Second, you need to break lines if a newline character comes.
Third, a text can be much larger than one page.
Here is a link to a tutorial. The result of this tutorial is a PrinterFramework, which can print much more than text.
Printer Framework Tutorial
At least the first two points are done, but not the last one. This is a bit more difficult. I did this, because I'm working on a printing API. But with extending the PrintingFramework a bit it is possible.
Hope this helps

Rene
khella smith
Ranch Hand

Joined: May 06, 2002
Posts: 39
thanks rene
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help with prints