The problem with the code below is that it doesn’t print of bk (Java Book) and only prints off ‘g’ which is the user interface window.
Can someone explain how to print off the Java Book?
public class printInvoice extends javax.swing.JFrame implements Printable {
JFrame frameToPrint;
/** Creates new form SimplePrint */
public void SimplePrint(JFrame f) {
frameToPrint = f;
}
/** Creates new form NewJFrame */
public printInvoice() {
initComponents();
this.setVisible(true);
print2();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new printInvoice().setVisible(true);
}
});
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex > 0) {
return (NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D) g;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
// Turn off double buffering
//componentToBePrinted.paint(g2d);
//frameToPrint.print(g);
paint(g);
// this.print(g);
// this.print(g);
// Turn double buffering back on
return (PAGE_EXISTS);
}
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JList jList1;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
int userCountAmount;
dataBase data = new dataBase();
Book bk = new Book();
PrinterJob PJ = PrinterJob.getPrinterJob();
public void print2() {
userCountAmount = data.getAmountOfUsers();
PageFormat portrait = PJ.defaultPage();
portrait.setOrientation(PageFormat.PORTRAIT);
jLabel1.setText("Print number: "+0);
bk.append((Printable) this, PJ.defaultPage(),1);
jLabel1.setText("Print number: "+1);
bk.append((Printable) this, PJ.defaultPage(),2);
jLabel1.setText("Print number: "+2);
bk.append((Printable) this, PJ.defaultPage(),3);
System.out.println(bk.toString());
//PageFormat PF = PJ.pageDialog(PJ.defaultPage());
PJ.setPageable(bk);
// PJ.setPrintable((Printable) this);
boolean doPrint = PJ.printDialog();
//JOptionPane.showMessageDialog(null, doPrint);
if (doPrint) {
try {
PJ.print();
} catch (PrinterException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
}
}
Printer job is using
print(Graphics g, PageFormat pageFormat, int pageIndex)when the book is printed. Yourprintmethod callspaint(g), and that is method of your UI frame.You shoud use
gof theprintmethod for drawing wanted images to pages, if you don’t want to print images of UI controls.EDIT: example, that draw text to middle of the page (not tested):
btw:
In your code, the following place need to be changed if you want print more pages than one: