Bonsoir , voilà je souhaiterai imprimer un rapport.
Pour cela , j'utilise un JTable.
Le souci c'est que si il est multi page ce rapport, j'ai besoin d'avoir un bouton sur lequel j'ai mis une action sinon ca ne marche pas. Même quand je fais copier coller de la fonction d'impression dans une fonction imprimer() que je lance dans le constructeur.

En plus , si je veux imprimer , je dois afficher ce Jtable sinon ca imprime vide

Voici mon code peut etre pourriez vous m'aider?
merci.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
 
/*
 * RepacsBErcyTemp.java
 *
 * Created on 22 décembre 2008, 10:19
 */
 
package com.fft.tribune;
 
import java.util.*;
import javax.swing.*;
import com.fft.tribune.tools.*;
import org.apache.log4j.Logger;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.MessageFormat;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.OrientationRequested;
 
/**
 *
 * @author  300329
 */
public class RepasBercyTemp extends javax.swing.JDialog{
 
 
    private static final Logger log = Logger.getLogger(LstJrnDialogVector.class);
 
    /** A return status code - returned if Cancel button has been pressed */
    public static final int RET_CANCEL = 0;
    /** A return status code - returned if OK button has been pressed */
    public static final int RET_OK = 1;
 
    private static final String query1_rg =
    	"select  I.SEXE_INV as Sexe, " +
                "I.NOM_INV+' '+I.PRENOM_INV as Invité, " +
                 "I.NOM_BENEF+' '+I.PRENOM_BENEF as Bénéficiaire, " +
                 "I.FONCTION_INV as Fonction,"+
                "I.USR_LOGIN as Créateur, " +
                "I.INV_TP, " +
                "I.INV_IMPRIME, " +
                "I.INV_VENU, " +
                "I.INV_VENU_SL, "+
                "H1.HOTE_CODE, " +
                "H2.HOTE_CODE, "+
                "I.TP_TYPE_INV "+
    	"from   V_TP_INVITES as I "+
        "join TP_HOTE as H1 on I.INV_MIDI = H1.HOTE_ID "+
        "join TP_HOTE as H2 on I.INV_SOIR = H2.HOTE_ID "+
    	"where  INV_CAL_JOUR = '";
 
      private static final String query_order_1 =
    	" order by H1.HOTE_CODE";
 
 
          private String queryPerTableMidi =       "select  "+
  "I.NOM_INV+' '+I.PRENOM_INV as Invité, "+
 "I.FONCTION_INV as Fonction, "+
  "I.NOM_BENEF+' '+I.PRENOM_BENEF as Bénéficiaire  "+
 
  "from   V_TP_INVITES as I join TP_HOTE as H1 on I.INV_MIDI = H1.HOTE_ID join TP_HOTE as H2 on I.INV_SOIR = H2.HOTE_ID "+
  "where  INV_CAL_JOUR = 'DAYCODE' "+
  "AND H1.HOTE_CODE = 'TABLE' "+
  "order by H1.HOTE_CODE,I.NOM_INV ";
 
      private String queryPerTableSoir =       "select  "+
  "I.NOM_INV+' '+I.PRENOM_INV as Invité, "+
 "I.FONCTION_INV as Fonction, "+
  "I.NOM_BENEF+' '+I.PRENOM_BENEF as Bénéficiaire  "+
 
  "from   V_TP_INVITES as I join TP_HOTE as H1 on I.INV_SOIR = H1.HOTE_ID join TP_HOTE as H2 on I.INV_SOIR = H2.HOTE_ID "+
  "where  INV_CAL_JOUR = 'DAYCODE' "+
  "AND H1.HOTE_CODE = 'TABLE' "+
  "order by H1.HOTE_CODE,I.NOM_INV ";
 
      private static final String query1_bercy =
    	"select  I.SEXE_INV as Sexe, " +
                "I.NOM_INV+' '+I.PRENOM_INV as Invité, " +
                "I.NOM_BENEF+' '+I.PRENOM_BENEF as Bénéficiaire, " +
                "H1.HOTE_CODE as Repas_Midi," +
                "H2.HOTE_CODE as Repas_Soir," +
                "I.FONCTION_INV as Fonction,"+
                "I.USR_LOGIN as Créateur " +
 
    	"from   V_TP_INVITES as I "+
        "join TP_HOTE as H1 on I.INV_MIDI = H1.HOTE_ID "+
        "join TP_HOTE as H2 on I.INV_SOIR = H2.HOTE_ID "+
    	"where  INV_CAL_JOUR = '";
 
    private String cal_id = null;;
    private ListeDesInvitationsModel qtm;
    Object elements[][] = null;
    private boolean isfirstTime;
    private ArrayList tables = new ArrayList();
    private String date = null;
    private String repas = null;
    private int nbLignes = 0;
 
 
 
    /** Creates new form JDialog */
    public RepasBercyTemp(javax.swing.JDialog parent, boolean modal , String repas , String cal_id, String date) {
        super(parent, modal);
        this.repas = repas;
        this.cal_id = cal_id;
        this.date = date;
 
         try{
            qtm = new ListeDesInvitationsModel();
         }catch(Exception e){
             System.out.println("erreur " + e.getMessage());
         }
 
        queryTableNames();
        initComponents();
        initJrnTable();
 
        lstJrnTablex.getColumn(0).setWidth(500);
        lstJrnTablex.getColumn(1).setWidth(500);
        lstJrnTablex.getColumn(2).setWidth(500);
        lstJrnTablex.setDefaultRenderer(Object.class,  new HeaderCellRenderer());
       // lstJrnTablex.setRowHeight(36);
        lstJrnTablex.packAll();
 
          setVisible(false);
          dispose();
          doClose(RET_OK);
 
 
 
    }
 
 
    /** @return the return status of this dialog - one of RET_OK or RET_CANCEL */
    public int getReturnStatus() {
        return returnStatus;
    }
 
     public void queryTableNames(){
 
         String temp = "";
         String field = "";
         if(repas.equals("MIDI")){
              temp = "select DISTINCT(H1.HOTE_CODE) as Repas_Midi "+
                           "from   V_TP_INVITES as I join TP_HOTE as H1 on I.INV_MIDI = H1.HOTE_ID join TP_HOTE as H2 on I.INV_SOIR = H2.HOTE_ID where  INV_CAL_JOUR = 'DAYCODE'" +
                           " AND FIELD <> '0'"+
                           " order by H1.HOTE_CODE";
 
              field = "H1.HOTE_CODE";
 
         }else{
              temp = "select  DISTINCT(H2.HOTE_CODE) as Repas_Soir "+
                           "from   V_TP_INVITES as I join TP_HOTE as H1 on I.INV_MIDI = H1.HOTE_ID join TP_HOTE as H2 on I.INV_SOIR = H2.HOTE_ID where  INV_CAL_JOUR = 'DAYCODE'" +
                           " AND FIELD <> '0'"+
                           " order by H2.HOTE_CODE";
 
              field = "H2.HOTE_CODE";
         }
 
 
         //On replace les paramètres dans la requete SQL
        temp = Functions.replace(temp, "DAYCODE", cal_id);
        temp = Functions.replace(temp, "FIELD", field);
        temp = Functions.replace(temp, "INV_TAGREPAS", "inv_"+repas);
 
         Connection c = ContextSingleton.getInstance().getConnection();
         ResultSet results;
 
 
        try {
            Statement stmt = c.createStatement();
            results = stmt.executeQuery(temp);
 
            while (results.next()) {
                Integer k = new Integer(2);
                String nom = results.getString(1);
                tables.add(nom);
             }
        }
 
        catch(Exception e){
            System.out.println("exception du a la requete");
 
        }
    }
 
public String defineQuery2(){
 
 
         date = ContextSingleton.getInstance().getFilterDate();
         String query = null;
         log.debug("event type" + ContextSingleton.getInstance().getEventType());
 
 
         if(ContextSingleton.getInstance().getEventType().equals("BERCY")){
 
                query = query1_bercy + ContextSingleton.getInstance().getFilterDate()+ "'" + query_order_1;
 
         }else if(ContextSingleton.getInstance().getEventType().equals("RG")){
         //else{
                query = query1_rg + ContextSingleton.getInstance().getFilterDate()+ "'";
         }
 
 
         log.debug("query liste des jours "+ query);
 
         if(!isfirstTime){
            nbInvites.setText(Integer.toString(lstJrnTablex.getRowCount()));
         }
         else{
             isfirstTime = true;
         }
 
 
         return query;
 
    }
 
    public void initJrnTable() {
 
 
 
 
 
        qtm.setQueryTotal("SELECT '' as col1, '' as col2, '' as col3");
 
        nbLignes++;
 
 
        int total = 0;
 
        for(int o = 0; o< tables.size(); o++){
 
            String[] blank = {"","",""};
            String table = "Table:"+(String)tables.get(o);
            log.debug( "ajout table " + table );
            String[] q = {table,"",""};
            String[] header = {"Invités","Fonction","Bénéficiaires"};
 
 
            //Nouvelle Table
            //On ajoute le nom de la table
            qtm.addLine(q);
            nbLignes++;
            qtm.addLine(header);
            nbLignes++;
 
            int subtotal = 0;
 
            if(repas.equals("MIDI")){
                    log.debug("c le midi");
                    String queryTable2 = Functions.replace(queryPerTableMidi, "DAYCODE", cal_id);
                    queryTable2 = Functions.replace(queryTable2, "TABLE", table.substring(6,table.length()));
                    log.debug(queryTable2);
 
                    subtotal = qtm.setQuery(queryTable2);
                    nbLignes =  nbLignes+subtotal ;
            }else{
                    log.debug("c le soir");
                    String queryTable2 = Functions.replace(queryPerTableSoir, "DAYCODE", cal_id);
                    queryTable2 = Functions.replace(queryTable2, "TABLE", table.substring(6,table.length()));
                    log.debug(queryTable2);
 
                    subtotal = qtm.setQuery(queryTable2);
                    nbLignes =  nbLignes+subtotal ;
            }
            total += subtotal;
            String[] subTotalLine = {"Total "+new Integer(subtotal).toString(),"",""};
             //Total
             qtm.addLine( subTotalLine );
             nbLignes++;
 
            //Lignes blanches
            qtm.addLine(blank);   nbLignes++;
            qtm.addLine(blank);   nbLignes++;
            qtm.addLine(blank);   nbLignes++;
        }
 
 
             String[] totalLine = {"Nombre Total D'Invités: "+new Integer(total).toString(),"",""};
             //Total
             qtm.addLine( totalLine );
             nbLignes++;
 
        lstJrnTablex.setModel(qtm);
        lstJrnTablex.setDefaultRenderer(String.class, new LstJrnStringRenderer());
        lstJrnTablex.setDefaultRenderer(Boolean.class, new LstJrnCheckBoxRenderer());
        LstJrnStringRenderer centerRenderer = new LstJrnStringRenderer();
        centerRenderer.setHorizontalAlignment(SwingConstants.CENTER);
 
 
    }
 
 
 
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
     private void initComponents() {
 
 
 
        okButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstJrnTablex = new org.jdesktop.swingx.JXTable();
        jLabel1 = new javax.swing.JLabel();
        scrollPane1 = new java.awt.ScrollPane();
        imprimerButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        nbInvites = new javax.swing.JLabel();
 
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });
 
 
        okButton.setText("Quitter");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
 
        lstJrnTablex.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
 
       javax.swing.JScrollPane jScrollPane1  = new javax.swing.JScrollPane ();
 
        jScrollPane2.setViewportView(lstJrnTablex);
        jLabel1.setFont(new java.awt.Font("MS Sans Serif", 0, 18));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Liste des Jours");
 
           jScrollPane1.setViewportView(lstJrnTablex);
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(133, 133, 133)
                        .addComponent(okButton))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 388, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(15, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(okButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
 
 
        pack();
    }
 
 
 
 
 
    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
           try {
                MessageFormat header = new MessageFormat(" Liste repas du "  + repas + " du "    + date );
                MessageFormat footer = new MessageFormat("Page {0,number,integer}");
                PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
                attr.add(OrientationRequested.PORTRAIT );
 
 
                if (! lstJrnTablex
                        .print(JTable.PrintMode.FIT_WIDTH, header, footer, false, attr , false )) {
                    System.out.println("User cancelled printing");
                }
            } catch (java.awt.print.PrinterException e) {
                    System.out.println("Cannot print" + e.getMessage());
            }
    }//GEN-LAST:event_okButtonActionPerformed
 
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        doClose(RET_CANCEL);
    }//GEN-LAST:event_closeDialog
 
    private void doClose(int retStatus) {
        returnStatus = retStatus;
        setVisible(false);
        dispose();
 
    }
 
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
 
        try { UIManager.setLookAndFeel(
                    //"com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                      UIManager.getCrossPlatformLookAndFeelClassName());
        } catch (Exception e) { }
 
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LstJrnDialogVector(new javax.swing.JFrame(), false).setVisible(false);
            }
        });
    }
 
 
    //Définit correctement la requête SQL
    public String defineQuery(){
 
         date = ContextSingleton.getInstance().getFilterDate();
         String query = null;
 
 
         if(ContextSingleton.getInstance().getEventType().equals("BERCY")){
 
                query = query1_bercy + ContextSingleton.getInstance().getFilterDate()+ "'" + query_order_1;
 
         }
 
         if(!isfirstTime){
            nbInvites.setText(Integer.toString(lstJrnTablex.getRowCount()));
         }
         else{
             isfirstTime = true;
         }
         return query;
    }
 
 
 
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.jdesktop.swingx.JXTable lstJrnTablex;
    private javax.swing.JButton imprimerButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable lstJrnTable;
    private javax.swing.JLabel nbInvites;
    private javax.swing.JButton okButton;
    private java.awt.ScrollPane scrollPane1;
    private javax.swing.JButton repasMidi;
    private javax.swing.JButton repasSoir;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
 
    private int returnStatus = RET_CANCEL;
}