Printing help

I磛e already posted an answer about this problem.

The question is:

I want to print pages with diferent text that is retrived from a jtable.

i磛e already made it for 1 page but when i want to print the second page i get an exact copy of the first page. What is the best method to print diferent pages without using the book class. Thanks...

[362 byte] By [jose.goncalvesa] at [2007-11-26 12:38:43]
# 1
Check out the PrintJob class http://java.sun.com/j2se/1.5.0/docs/api/
FortisVenalitera at 2007-7-7 16:07:57 > top of Java-index,Security,Cryptography...
# 2

Thanks but i磎 already using this method:

package sqlconnector;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.print.PageFormat;

import java.awt.print.Printable;

import java.awt.print.PrinterException;

import java.awt.print.PrinterJob;

import javax.swing.JOptionPane;

/*

* TesteParaImprimir.java

*

* Created on 12 de Dezembro de 2006, 22:46

*/

/**

*

* @author luis.martins

*/

public class TesteParaImprimir extends javax.swing.JFrame implements Printable{

/** Creates new form TesteParaImprimir */

public TesteParaImprimir() {

initComponents();

}

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents

private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();

jTable1 = new javax.swing.JTable();

jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jTable1.setModel(new javax.swing.table.DefaultTableModel(

new Object [][] {

{"a", null},

{"b", null},

{"c", null},

{"d", null},

{"e", null},

{"f", null},

{"g", null},

{"h", null},

{"i", null},

{"j", null},

{"k", null},

{"l", null},

{"m", null},

{"n", null},

{"o", null},

{"p", null},

{"q", null},

{"r", null},

{"s", null},

{"t", null},

{"u", null},

{"v", null},

{"w", null},

{"x", null}

},

new String [] {

"Nome", "Imprimir"

}

) {

Class[] types = new Class [] {

java.lang.Object.class, java.lang.Boolean.class

};

public Class getColumnClass(int columnIndex) {

return types [columnIndex];

}

});

jScrollPane1.setViewportView(jTable1);

jButton1.setText("Imprimir");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton1ActionPerformed(evt);

}

});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

.addComponent(jButton1)

.addContainerGap())))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)

.addComponent(jButton1)

.addContainerGap())

);

pack();

}// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

this.Imprimir();

}//GEN-LAST:event_jButton1ActionPerformed

public void Imprimir(){

PrinterJob job = PrinterJob.getPrinterJob();

job.setPrintable(this);

boolean ok = job.printDialog();

if (ok) {

try {

job.print();

} catch (PrinterException ex) {

}

}

}

public int print(Graphics g, PageFormat pf, int pi) {

Graphics2D g2 = (Graphics2D) g;

int totalNumPages=2;

if(pi>=totalNumPages) {return NO_SUCH_PAGE;}

g2.translate(pf.getImageableX(),

pf.getImageableY());

for(int i = 0;i<10;++i){

JOptionPane.showMessageDialog(null,"Etiqueta"+i);

g2.drawString("Nome"+i,10,i*100);

}

return PAGE_EXISTS;

}

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new TesteParaImprimir().setVisible(true);

}

});

}

// Variables declaration - do not modify//GEN-BEGIN:variables

private javax.swing.JButton jButton1;

private javax.swing.JScrollPane jScrollPane1;

private javax.swing.JTable jTable1;

// End of variables declaration//GEN-END:variables

private int numeroDePaginas=2;

private int guardarValor=0;

}

My problem is that it still prints 2 exact copies of the first page. Can you help me? By the way i磎 using NetBeans. Thanks

jose.goncalvesa at 2007-7-7 16:07:57 > top of Java-index,Security,Cryptography...