1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
import javax.swing.*;
import javax.swing.event.*;
import java.awt.print.*;
public class Table extends JPanel implements Printable {
private JTable table;
private MonTableModel tableModel;
private JPanel globalPanel = new JPanel();
public Table() {
super();
this.setLayout(new BorderLayout());
globalPanel.setLayout(new BorderLayout());
table = new JTable();
JScrollPane scrollPane = new JScrollPane(table);
tableModel = new MonTableeModel();
table.setModel(tableModel);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
globalPanel.add(scrollPane,BorderLayout.CENTER);
this.add(globalPanel,BorderLayout.CENTER);
}
[
]
/**
* méthodes permettant d'imprimer la JTable
*/
public void imprimer(){
PrinterJob pj = PrinterJob.getPrinterJob();
//PageFormat pf = pj.pageDialog(pj.defaultPage());
PageFormat landscape = pj.defaultPage();
landscape.setOrientation(PageFormat.LANDSCAPE);
pj.setPrintable(this,landscape);
if( pj.printDialog()){
try{
pj.print();
}
catch (Exception PrintException) { }
}
}
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 fontDescent = g2.getFontMetrics().getDescent();
// laisser de l'espace pour le numero de page
double pageHeight = pageFormat.getImageableHeight() - fontHeight;
double pageWidth = pageFormat.getImageableWidth();
double tableWidth = (double)table.getColumnModel().getTotalColumnWidth();
double scale = 1;
if (tableWidth >= pageWidth)
{
scale = pageWidth / tableWidth;
}
double headerHeightOnPage = table.getTableHeader().getHeight()*scale;
double tableWidthOnPage = tableWidth * scale;
double oneRowHeight = (table.getRowHeight() + table.getRowMargin())*scale;
int numRowsOnAPage = (int)((pageHeight-headerHeightOnPage) / oneRowHeight);
double pageHeightForTable = oneRowHeight * numRowsOnAPage;
int totalNumPages = (int)Math.ceil(((double)table.getRowCount()) / numRowsOnAPage);
if (pageIndex >= totalNumPages)
return NO_SUCH_PAGE;
g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
// en bas au centre
g2.drawString("Page: " + (pageIndex +1),(int)pageWidth/2 - 35, (int)(pageHeight + fontHeight - fontDescent));
g2.translate(0f, headerHeightOnPage);
g2.translate(0f, -pageIndex*pageHeightForTable);
// si cette partie de la page est plus petite
// que la taille disponible, alors découper les contours appropriés
if (pageIndex + 1 == totalNumPages)
{
int lastRowPrinted = numRowsOnAPage * pageIndex;
int numRowsLeft = table.getRowCount() - lastRowPrinted;
g2.setClip(0, (int)(pageHeightForTable * pageIndex),(int)Math.ceil(tableWidthOnPage),(int)Math.ceil(oneRowHeight * numRowsLeft));
}
// sinon découper la zone disponible toute entière
else
{
g2.setClip(0, (int)(pageHeightForTable * pageIndex),(int)Math.ceil(tableWidthOnPage),(int)Math.ceil(pageHeightForTable));
}
g2.scale(scale, scale);
table.paint(g2);
// dessiner les entêtes en haut
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);
table.getTableHeader().paint(g2);
return Printable.PAGE_EXISTS;
}
} |