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
| import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRMapCollectionDataSource;
import net.sf.jasperreports.engine.data.JRTableModelDataSource;
import net.sf.jasperreports.view.JasperViewer;
public class Subrep {
static DefaultTableModel mod;
public static void main(String[] args) {
JFrame f = new JFrame();
f.setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(0, 0, 400, 200);
String title[] = {"nom", "prenom"};
String data[][] = {{"A", "B"}, {"A", "B"}};
mod = new DefaultTableModel(data,title);
JTable tab = new JTable(mod);
JScrollPane sc=new JScrollPane(tab);
sc.setBounds(10, 10, 370, 80);
f.add(sc);
JButton b = new JButton("imprimer");
b.setBounds(150, 110, 100, 25);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
//****************************************
JRTableModelDataSource jrtAL = new JRTableModelDataSource(mod);
Map SUB_DATA1 = new HashMap();
SUB_DATA1.put("jrtAL", jrtAL);
Map<String, Object> param = new HashMap<String, Object>();
param.put("SUB_DATA1", SUB_DATA1);
String filename = "Rapport.jasper";
String subreport = "SubRapport.jasper";
InputStream jasperReport = null;
String subR = this.getClass().getResource(subreport).getPath();
param.put("SUBREPORT_DIR", subR);
Map simpleMasterMap = new HashMap();
simpleMasterMap.put("rcc", "RC N°: ");
List simpleMasterList = new ArrayList();
simpleMasterList.add(simpleMasterMap);
JRMapCollectionDataSource simpleDS = new JRMapCollectionDataSource(simpleMasterList);
jasperReport = this.getClass().getResourceAsStream(filename);
JasperPrint print = JasperFillManager.fillReport(jasperReport, param, simpleDS);
JasperViewer.viewReport(print, false);
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(new JFrame(), ex.getMessage());
}
}
});
f.add(b);
f.setVisible(true);
}
} |
Partager