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
| import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTable.PrintMode;
public class testFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JTable table;
public testFrame() throws HeadlessException {
super();
Vector<String> vectName= new Vector<String>();
Vector< Vector<Integer>> vectRow = new Vector<Vector<Integer>>();
for(int i=0;i<20;i++){
vectName.add(""+i);
Vector<Integer> v =new Vector<Integer>();
for(int j=0;j<20;j++){
v.add(i*j);
}
vectRow.add(v);
}
JScrollPane jsp= new JScrollPane();
table = new JTable(vectRow,vectName);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);//
jsp.setViewportView(table);
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JButton buttonPrint = new JButton("Imprimer");
buttonPrint.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
printTable();
}
});
this.setLayout(new BorderLayout());
this.add(buttonPrint,BorderLayout.SOUTH);
this.add(jsp,BorderLayout.CENTER);
this.setVisible(true);
this.setSize(new Dimension(800,600));
}
protected void printTable() {
PrinterJob job = PrinterJob.getPrinterJob();
//Creation du format des pages
PageFormat pop = job.defaultPage();
//mise en paysage
pop.setOrientation(PageFormat.LANDSCAPE);
job.setPrintable(table.getPrintable(PrintMode.FIT_WIDTH,null,null),pop);
if(job.printDialog()){
try {
job.print();
} catch (PrinterException e) {
JOptionPane.showMessageDialog(this,"Erreur d'impression :\n"+e.getMessage(),"Erreur",JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
}
public static void main(String[] args){
new testFrame();
}
} |
Partager