pior que eu tive que fazer algo parecido…só que não utilizei o jasper ireport vou postar aqui um demo pra ver se te ajuda
[code]/*
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package rx;
/**
*
-
@author Udi
/
import Banco.Conexao;
import java.awt.;
import java.awt.font.;
import java.awt.geom.;
import java.awt.print.*;
public class Impressao {
Conexao conEtiq;
//— Private instances declarations
private final static int POINTS_PER_INCH = 72;
private String nome, convenio, tecnico,alias1, alias2, alias3, alias4,
alias5, alias6, alias7, alias8, alias9, alias10, ultimoAlias;
/**
* Constructor: Example2
*
*/
public Impressao () {
// RepaintManager currentManager = RepaintManager.currentManager©;
// currentManager.setDoubleBufferingEnabled(false);
//-- Conecta o banco de dados
conEtiq = new Conexao();
conEtiq.conecta();
//----------------------------
//--- Create a new PrinterJob object
PrinterJob printJob = PrinterJob.getPrinterJob ();
//--- Create a new book to add pages to
Book book = new Book ();
//--- Add the cover page using the default page format for this print job
book.append (new IntroPage (), printJob.defaultPage ());
//--- Add the document page using a landscape page format
PageFormat documentPageFormat = new PageFormat ();
documentPageFormat.setOrientation (PageFormat.LANDSCAPE);
book.append (new Document (), documentPageFormat);
//--- Tell the printJob to use the book as the pageable object
printJob.setPageable (book);
//--- Show the print dialog box. If the user click the
//--- print button we then proceed to print else we cancel
//--- the process.
if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception PrintException) {
PrintException.printStackTrace();
}
}
// currentManager.setDoubleBufferingEnabled(true);
}
/**
* Class: IntroPage
*
* This class defines the painter for the cover page by implementing the
* Printable interface.
*
*/
private class IntroPage implements Printable {
/**
* Method: print <p>
*
* @param g a value of type Graphics
* @param pageFormat a value of type PageFormat
* @param page a value of type int
* @return a value of type int
*/
public int print (Graphics g, PageFormat pageFormat, int page) {
//--- Create the Graphics2D object
Graphics2D g2d = (Graphics2D) g;
//--- Translate the origin to 0,0 for the top left corner
g2d.translate (pageFormat.getImageableX (), pageFormat.getImageableY ());
//--- Set the default drawing color to black
g2d.setPaint (Color.black);
//--- Draw a border arround the page
//Rectangle2D.Double border = new Rectangle2D.Double (0,
// 0,
// pageFormat.getImageableWidth (),
// pageFormat.getImageableHeight ());
//g2d.draw (border);
//--- Print the title
String Cabecalho = "Etiqueta";
//String titleText = "Printing in Java Part 2";
//Font titleFont = new Font ("helvetica", Font.BOLD, 12);
Font titleFont = new Font ("helvetica", Font.TRUETYPE_FONT, 11);
g2d.setFont (titleFont);
//--- Compute the horizontal center of the page
//FontMetrics fontMetrics = g2d.getFontMetrics ();
//double titleX = (pageFormat.getImageableWidth () / 2) - (fontMetrics.stringWidth (titleText) / 2);
//double titleY = 3 * POINTS_PER_INCH;
double x = 15;
double y = 30;
//g2d.drawString (titleText, (int) titleX, (int) titleY);
g2d.drawString (Cabecalho, (int) x, (int) y);
try{
conEtiq.executeSQL("SELECT * FROM tbl_etiqueta");
conEtiq.rs.last();
ultimoAlias = conEtiq.rs.getString("varchar_alias_etiqueta");
conEtiq.rs.first();
nome = conEtiq.rs.getString("varchar_nome_etiqueta");
convenio = conEtiq.rs.getString("varchar_convenio_etiqueta");
alias1 = conEtiq.rs.getString("varchar_alias_etiqueta");
tecnico = conEtiq.rs.getString("varchar_tecnico_etiqueta");
g2d.drawString ("Nome:", (int) x, (int) y+20);
g2d.drawString (nome, 52, (int) y+20);
g2d.drawString ("Convênio:", (int) x, (int) y+30);
g2d.drawString (convenio, 72, (int) y+30);
g2d.drawString ("Técnico:", (int) x, (int) y+40);
g2d.drawString (tecnico, 71, (int) y+40);
g2d.drawString ("Alias:", (int) x, (int) y+50);
g2d.drawString (alias1, 47, (int) y+50);
conEtiq.rs.next();
alias2 = conEtiq.rs.getString("varchar_alias_etiqueta");
if(alias1.equals(ultimoAlias)){
// return (PAGE_EXISTS);
}else{
g2d.drawString (",", 78, (int) y+50);
g2d.drawString (alias2, 83, (int) y+50);
}
conEtiq.rs.next();
alias3 = conEtiq.rs.getString("varchar_alias_etiqueta");
if(alias2.equals(ultimoAlias)){
//return (PAGE_EXISTS);
}else{
g2d.drawString (",", 113, (int) y+50);
g2d.drawString (alias3, 118, (int) y+50);
}
conEtiq.rs.next();
alias4 = conEtiq.rs.getString("varchar_alias_etiqueta");
if(alias3.equals(ultimoAlias)){
//return (PAGE_EXISTS);
}else{
g2d.drawString (",", 150, (int) y+50);
g2d.drawString (alias4, 153, (int) y+50);
}
conEtiq.rs.next();
alias5 = conEtiq.rs.getString("varchar_alias_etiqueta");
if(alias4.equals(ultimoAlias)){
//return (PAGE_EXISTS);
}else{
g2d.drawString (",", 183, (int) y+50);
g2d.drawString (alias5, 188, (int) y+50);
}
//-- move a impressao para proxima linha se tiver mais de 5 alias
conEtiq.rs.next();
alias6 = conEtiq.rs.getString("varchar_alias_etiqueta");
if(alias5.equals(ultimoAlias)){
//return (PAGE_EXISTS);
}else{
//g2d.drawString (",", 15, (int) y+60);
g2d.drawString (alias6, 16, (int) y+60);
}
//g2d.drawString (titleText, (int) titleX, (int) titleY);
} catch (Exception e) { }
return (PAGE_EXISTS);
}
}
/**
* Class: Document
*
* This class is the painter for the document content.
*
*/
private class Document implements Printable {
/**
* Method: print <p>
*
* @param g a value of type Graphics
* @param pageFormat a value of type PageFormat
* @param page a value of type int
* @return a value of type int
*/
public int print (Graphics g, PageFormat pageFormat, int page) {
//--- Create the Graphics2D object
Graphics2D g2d = (Graphics2D) g;
//--- Translate the origin to 0,0 for the top left corner
g2d.translate (pageFormat.getImageableX (), pageFormat.getImageableY ());
//--- Set the drawing color to black
g2d.setPaint (Color.black);
//--- Draw a border arround the page using a 12 point border
//g2d.setStroke (new BasicStroke (12));
//Rectangle2D.Double border = new Rectangle2D.Double (0,
// 0,
// pageFormat.getImageableWidth (),
// pageFormat.getImageableHeight ());
//g2d.draw (border);
//--- Print the text one inch from the top and left margins
//g2d.drawString ("This the content page", POINTS_PER_INCH, POINTS_PER_INCH);
//double x = 15;
//double y = 50;
//g2d.drawString (titleText, (int) titleX, (int) titleY);
//g2d.drawString ("Nome:", (int) x, (int) y);
//g2d.drawString (nome, (int) x, (int) y+10);
//g2d.drawString ("Convênio:", (int) x+10, (int) y);
//g2d.drawString (convenio, (int) x, (int) y+10);
//g2d.drawString ("Alias:", (int) x+10, (int) y);
//g2d.drawString (alias, (int) x, (int) y+10);
//--- Validate the page
return (PAGE_EXISTS);
}
}
} // Example2
[/code]