IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Composants Java Discussion :

Aperçu Avant impression sur une JTable


Sujet :

Composants Java

  1. #1
    Membre habitué
    Étudiant
    Inscrit en
    Janvier 2009
    Messages
    174
    Détails du profil
    Informations personnelles :
    Âge : 37

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2009
    Messages : 174
    Points : 140
    Points
    140
    Par défaut Aperçu Avant impression sur une JTable
    Bonjour tous le monde,

    J'ai une application qui utilise beaucoup de tables je veut savoir si je peut avoir un aperçu avant impression de Mes tables.

    Merci d'avance.

  2. #2
    Expert confirmé
    Avatar de slim_java
    Homme Profil pro
    Enseignant
    Inscrit en
    Septembre 2008
    Messages
    2 272
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Septembre 2008
    Messages : 2 272
    Points : 4 539
    Points
    4 539
    Par défaut
    tu dois te documenter sur javax.swing.JTable.PrintMode

  3. #3
    Membre habitué
    Étudiant
    Inscrit en
    Janvier 2009
    Messages
    174
    Détails du profil
    Informations personnelles :
    Âge : 37

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2009
    Messages : 174
    Points : 140
    Points
    140
    Par défaut
    voila la classe qui permet d'avoir un aperçu sur un Table :


    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
    public class PrintPreview extends JDialog implements Runnable
    {
        protected JScrollPane displayArea;
        protected int m_wPage;
        protected int m_hPage;
        protected int width;
        protected int height;
        protected Printable m_target;
        protected JComboBox m_cbScale;
        protected PreviewContainer m_preview;
        protected PageFormat pp_pf = null;
        protected JButton formatButton;
        protected JButton shrinkButton;
        @SuppressWarnings("unused")
    	private static final int INCH = 72;
        private static boolean bScallToFitOnePage = false;
        private String msg;
        JTable table;
     
        protected void getThePreviewPages()
        {
            m_wPage = (int)(pp_pf.getWidth());
            m_hPage = (int)(pp_pf.getHeight());
            int scale = getDisplayScale();
            width = (int)Math.ceil(m_wPage*scale/100);
            height = (int)Math.ceil(m_hPage*scale/100);
     
            int pageIndex = 0;
            try {
              while (true) {
                BufferedImage img = new BufferedImage(m_wPage, m_hPage,
                                                  BufferedImage.TYPE_INT_RGB);
                Graphics g = img.getGraphics();
                g.setColor(Color.white);
                g.fillRect(0, 0, m_wPage, m_hPage);
                if (bScallToFitOnePage) {
                    m_target.print(g, pp_pf, -1);
                    PagePreview pp = new PagePreview(width, height, img);
                    m_preview.add(pp);
                    break;
                } else
                if (m_target.print(g, pp_pf, pageIndex) !=  Printable.PAGE_EXISTS)
                    break;
                PagePreview pp = new PagePreview(width, height, img);
                m_preview.add(pp);
                pageIndex++;
              }
            } catch (OutOfMemoryError om) {
                JOptionPane.showMessageDialog(this,
                    "image is too big that run out of memory.", "Print Preview",
                    JOptionPane.INFORMATION_MESSAGE);
            }
            catch (PrinterException e) {
                e.printStackTrace();
                System.err.println("Printing error: "+e.toString());
            }
        }
     
        protected void previewThePages(int orientation)
        {
            if (displayArea != null) displayArea.setVisible(false);
     
            m_preview = new PreviewContainer();
     
            getThePreviewPages();
     
            displayArea = new JScrollPane(m_preview);
            getContentPane().add(displayArea, BorderLayout.CENTER);
            setLocationRelativeTo(null);
            setVisible(true);
            System.gc();
        }
     
     
        protected void createButtons(JToolBar tb, boolean shrink) {
            JButton bt = new JButton("Print", new ImageIcon("print.gif"));
            ActionListener lst = new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                try {
     
                	MessageFormat header = null;   
                    MessageFormat footer = null;
                    footer = new MessageFormat("Page {0}");
     
                    boolean fitWidth = true;
                    boolean showPrintDialog = true;
                    boolean interactive = true;
     
                    JTable.PrintMode mode = fitWidth ? JTable.PrintMode.FIT_WIDTH
                                                 : JTable.PrintMode.NORMAL;
     
                    @SuppressWarnings("unused")
    				boolean complete=false;
                    header = new MessageFormat(""+msg);
        			complete = table.print(mode, header, footer,
                                             showPrintDialog, null,
                                             interactive, null);
                   /*/ PrinterJob prnJob = PrinterJob.getPrinterJob();
                    prnJob.setPrintable(m_target, pp_pf);
                    if (prnJob.printDialog()) {
                        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                        prnJob.print();
                        setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    }*/
                    dispose();
                }
                catch (PrinterException ex) {
                    ex.printStackTrace();
                    System.err.println("Printing error: "+ex.toString());
                }
              }
            };
            bt.addActionListener(lst);
            bt.setAlignmentY(0.5f);
            bt.setMargin(new Insets(4,6,4,6));
            tb.add(bt);
     
            if (pp_pf.getOrientation() == PageFormat.PORTRAIT)
                formatButton = new JButton("Landscape");
            else
                formatButton = new JButton("Portrait");
     
            lst = new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  if (pp_pf.getOrientation() == PageFormat.PORTRAIT) {
                    pp_pf.setOrientation(PageFormat.LANDSCAPE);
                    previewThePages(PageFormat.LANDSCAPE);
                    formatButton.setText("Portrait");
                  } else {
                    pp_pf.setOrientation(PageFormat.PORTRAIT);
                    previewThePages(PageFormat.PORTRAIT);
                    formatButton.setText("Landscape");
                  }
              }
            };
            formatButton.addActionListener(lst);
            formatButton.setAlignmentY(0.5f);
            formatButton.setMargin(new Insets(4,6,4,6));
            tb.add(formatButton);
     
            if (shrink) {
                shrinkButton = new JButton("Shrink to fit");
     
                lst = new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      bScallToFitOnePage = !bScallToFitOnePage;
                      previewThePages(pp_pf.getOrientation());
                  }
                };
                shrinkButton.addActionListener(lst);
                shrinkButton.setAlignmentY(0.5f);
                shrinkButton.setMargin(new Insets(4,6,4,6));
                tb.add(shrinkButton);
            }
     
            bt = new JButton("Close");
            lst = new ActionListener() {
              public void actionPerformed(ActionEvent e) {
     
                dispose();
              }
            };
            bt.addActionListener(lst);
            bt.setAlignmentY(0.5f);
            bt.setMargin(new Insets(4,6,4,6));
            tb.add(bt);
        }
     
        public int getDisplayScale() {
            String str = m_cbScale.getSelectedItem().toString();
            if (str.endsWith("%")) str = str.substring(0, str.length()-1);
            str = str.trim();
            int scale = 0;
            try { scale = Integer.parseInt(str); }
                catch (NumberFormatException ex) { return 25; }
            return scale ;
        }
     
      public PrintPreview(Printable target,JTable table2,String Message) {
        this(target, "Print Preview", false, table2,Message);
      }
     
      public PrintPreview(Printable target, String title,JTable table2,String Message) {
        this(target, title, false, table2, Message);
      }
      public PrintPreview(Printable target, String title, boolean shrink,JTable table2,String Message) {
        setTitle(title);
        table=table2;
        msg=Message;
        setIconImage( new ImageIcon( "teloptica.gif" ).getImage() );
        bScallToFitOnePage = false;  // reset to default
        PrinterJob prnJob = PrinterJob.getPrinterJob();
        pp_pf = prnJob.defaultPage();
        if (pp_pf.getHeight()==0 || pp_pf.getWidth()==0) {
            System.err.println("Unable to determine default page size");
            return;
        }
        setSize(670, 750);
        m_target = target;
     
        displayArea = null;
        m_preview = null;
     
        JToolBar tb = new JToolBar();
        createButtons(tb, shrink);
     
        String[] scales = { "10 %", "25 %", "50 %", "100 %" };
        m_cbScale = new JComboBox(scales);
        m_cbScale.setSelectedIndex(3);
        ActionListener lst = new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            Thread runner = new Thread(PrintPreview.this);
            runner.start();
          }
        };
        m_cbScale.addActionListener(lst);
        m_cbScale.setMaximumSize(m_cbScale.getPreferredSize());
        m_cbScale.setEditable(true);
        tb.addSeparator();
        tb.add(m_cbScale);
        getContentPane().add(tb, BorderLayout.NORTH);
     
    //    previewThePages(PageFormat.PORTRAIT);
        previewThePages(pp_pf.getOrientation());
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setResizable(false);
        setModal(true);
        setVisible(true);
      }
     
      public void run() {
          int scale = getDisplayScale();
          width = (int)(m_wPage*scale/100);
          height = (int)(m_hPage*scale/100);
     
          Component[] comps = m_preview.getComponents();
          for (int k=0; k<comps.length; k++) {
              if (!(comps[k] instanceof PagePreview))
                  continue;
              PagePreview pp = (PagePreview)comps[k];
              pp.setScaledSize(width, height);
          }
          m_preview.doLayout();
          m_preview.getParent().getParent().validate();
      }
     
      class PreviewContainer extends JPanel
      {
        protected int H_GAP = 16;
        protected int V_GAP = 10;
     
        public Dimension getPreferredSize() {
          int n = getComponentCount();
          if (n == 0)
            return new Dimension(H_GAP, V_GAP);
          Component comp = getComponent(0);
          Dimension dc = comp.getPreferredSize();
          int w = dc.width;
          int h = dc.height;
     
          Dimension dp = getParent().getSize();
          int nCol = Math.max((dp.width-H_GAP)/(w+H_GAP), 1);
          int nRow = n/nCol;
          if (nRow*nCol < n)
            nRow++;
     
          int ww = nCol*(w+H_GAP) + H_GAP;
          int hh = nRow*(h+V_GAP) + V_GAP;
          Insets ins = getInsets();
          return new Dimension(ww+ins.left+ins.right,
            hh+ins.top+ins.bottom);
        }
     
        public Dimension getMaximumSize() {
          return getPreferredSize();
        }
     
        public Dimension getMinimumSize() {
          return getPreferredSize();
        }
     
        public void doLayout() {
          Insets ins = getInsets();
          int x = ins.left + H_GAP;
          int y = ins.top + V_GAP;
     
          int n = getComponentCount();
          if (n == 0)
            return;
          Component comp = getComponent(0);
          Dimension dc = comp.getPreferredSize();
          int w = dc.width;
          int h = dc.height;
     
          Dimension dp = getParent().getSize();
          int nCol = Math.max((dp.width-H_GAP)/(w+H_GAP), 1);
          int nRow = n/nCol;
          if (nRow*nCol < n)
            nRow++;
     
          int index = 0;
          for (int k = 0; k<nRow; k++) {
            for (int m = 0; m<nCol; m++) {
              if (index >= n)
                return;
              comp = getComponent(index++);
              comp.setBounds(x, y, w, h);
              x += w+H_GAP;
            }
            y += h+V_GAP;
            x = ins.left + H_GAP;
          }
        }
      }
     
      class PagePreview extends JPanel
      {
        protected int m_w;
        protected int m_h;
        protected Image m_source;
        protected Image m_img;
     
        public PagePreview(int w, int h, Image source) {
          m_w = w;
          m_h = h;
          m_source= source;
          m_img = m_source.getScaledInstance(m_w, m_h, Image.SCALE_SMOOTH);
          m_img.flush();
          setBackground(Color.white);
          setBorder(new MatteBorder(1, 1, 2, 2, Color.black));
        }
     
        public void setScaledSize(int w, int h) {
          m_w = w;
          m_h = h;
          m_img = m_source.getScaledInstance(m_w, m_h, Image.SCALE_SMOOTH);
          repaint();
        }
     
        public Dimension getPreferredSize() {
          Insets ins = getInsets();
          return new Dimension(m_w+ins.left+ins.right,  m_h+ins.top+ins.bottom);
        }
     
        public Dimension getMaximumSize() {
          return getPreferredSize();
        }
     
        public Dimension getMinimumSize() {
          return getPreferredSize();
        }
     
        public void paint(Graphics g) {
          g.setColor(getBackground());
          g.fillRect(0, 0, getWidth(), getHeight());
          g.drawImage(m_img, 0, 0, this);
          paintBorder(g);
        }
      }
    }
    merci pour l'aide.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [XL-2003] Aperçu avant impression d'une plage non vide
    Par mikadoo57 dans le forum Macros et VBA Excel
    Réponses: 20
    Dernier message: 06/01/2013, 20h30
  2. [XL-2003] Aperçu avant impression d'une plage non vide
    Par mikadoo57 dans le forum Contribuez
    Réponses: 0
    Dernier message: 05/01/2013, 16h13
  3. Aperçu avant impression d'une JTable
    Par Polux000 dans le forum Composants
    Réponses: 4
    Dernier message: 03/10/2012, 15h32
  4. [XL-2007] créer un bouton aperçu avant impression sur Excel
    Par hollywoodsky dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 02/12/2009, 17h44
  5. Comment réaliser un aperçu avant impression avec une table ?
    Par Yoni Lebene dans le forum Bases de données
    Réponses: 13
    Dernier message: 17/12/2007, 10h56

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo