Seguendo il tutorial java ho implementato una classe che estende la JTable ed è stampabile; ho però un inconveniente: mentre tento di lanciare la stampa il pc si impalla e il metodo print() entra in un ciclo infinito.
Se cambio il metodo print con un altro di test invece riesco a stampare una stringa, quindi il problema non risiede nell'errata invocazione del metodo.
Sapete come risolvere questo maledetto problema?![]()
![]()
Ecco il codice della classe (il metodo print è copiato direttamente dal tutorial consigliato nel post precedente):
codice:import javax.swing.table.*; import javax.swing.*; //Per Stampa import java.awt.print.*; import java.util.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.Dimension; public class ClientiJTable extends JTable implements Printable{ public ClientiJTable(){ TableModel dataModel = new AbstractTableModel(){ public int getColumnCount(){ return 5; } public int getRowCount(){ return 100; } public Object getValueAt(int row, int col){ return new Integer(row*col); } }; this.setModel(dataModel); RepaintManager.currentManager(this).setDoubleBufferingEnabled(false); } public int print(Graphics g, PageFormat pageFormat,int pageIndex) throws PrinterException { Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.black); int fontHeight=g2.getFontMetrics().getHeight(); int fontDesent=g2.getFontMetrics().getDescent(); //leave room for page number double pageHeight = pageFormat.getImageableHeight()-fontHeight; double pageWidth = pageFormat.getImageableWidth(); double tableWidth = (double)this.getColumnModel().getTotalColumnWidth(); double scale = 1; if (tableWidth >= pageWidth) { scale = pageWidth / tableWidth; } double headerHeightOnPage = this.getTableHeader().getHeight()*scale; double tableWidthOnPage = tableWidth*scale; double oneRowHeight = (this.getRowHeight()+this.getRowMargin())*scale; int numRowsOnAPage = (int)((pageHeight-headerHeightOnPage)/oneRowHeight); double pageHeightForTable=oneRowHeight*numRowsOnAPage; int totalNumPages = (int)Math.ceil(((double)this.getRowCount())/numRowsOnAPage); if(pageIndex>=totalNumPages) { return NO_SUCH_PAGE; } g2.translate(pageFormat.getImageableX(),pageFormat.getImageableY()); //bottom center g2.drawString("Page: "+(pageIndex+1),(int)pageWidth/2-35,(int)(pageHeight+fontHeight-fontDesent)); g2.translate(0f,headerHeightOnPage); g2.translate(0f,-pageIndex*pageHeightForTable); //If this piece of the table is smaller //than the size available, //clip to the appropriate bounds. if (pageIndex + 1 == totalNumPages) { int lastRowPrinted = numRowsOnAPage * pageIndex; int numRowsLeft = this.getRowCount() - lastRowPrinted; g2.setClip(0,(int)(pageHeightForTable * pageIndex),(int) Math.ceil(tableWidthOnPage),(int) Math.ceil(oneRowHeight*numRowsLeft)); } //else clip to the entire area available. else{ g2.setClip(0,(int)(pageHeightForTable*pageIndex),(int) Math.ceil(tableWidthOnPage),(int) Math.ceil(pageHeightForTable)); } g2.scale(scale,scale); this.paint(g2); g2.scale(1/scale,1/scale); g2.translate(0f,pageIndex*pageHeightForTable); g2.translate(0f, -headerHeightOnPage); g2.setClip(0, 0, (int) Math.ceil(tableWidthOnPage), (int)Math.ceil(headerHeightOnPage)); g2.scale(scale,scale); this.getTableHeader().paint(g2); //paint header at top return Printable.PAGE_EXISTS; } /* public int print(Graphics grap,PageFormat pageFormat, int pageIndex) throws PrinterException{ if(pageIndex > 0) return NO_SUCH_PAGE; grap.drawString("Prova di stampa!", (int)pageFormat.getImageableX(),(int)pageFormat.getImageableY()+5); grap.setColor(Color.blue); grap.drawLine((int)pageFormat.getImageableX(), (int)pageFormat.getImageableY(),(int)pageFormat.getImageableWidth(),(int)pageFormat.getImageableHeight()); return PAGE_EXISTS; } */ }![]()

Rispondi quotando