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 :

Imprimer une JTable


Sujet :

Composants Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé Avatar de yashiro
    Inscrit en
    Mars 2004
    Messages
    214
    Détails du profil
    Informations forums :
    Inscription : Mars 2004
    Messages : 214
    Par défaut Imprimer une JTable
    Bonjour à tous,
    Je suis en phase de finalisation d'une application java (Eclipse 3.2 + J2SE 1.5) de gestion de ressource. Dans cette application, j'affiche un emploi de temps sous forme de JTable relooké et j'y matérialise les tâches par des rectangle transparents au-dessus des cellules du JTable.¨Pour cela, je redéfinit la méthode paintComponent(Graphics g) du JTable et j'utilise à l'interieur un graphics2D pour la transparence
    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
     
    public void paintComponent(Graphics g){
    		super.paintComponents(g);
     
    		int width = getWidth();
    		int height = getHeight();
    		Graphics2D g2d = (Graphics2D)g; // Ligne d'erreur
     
    		Paint old = g2d.getPaint(); // Sauvegarde de l'anien mode de peinture
     
    		// Création du gradient de peinture
    		GradientPaint gp = new GradientPaint(0, 0, color, width, height, color.brighter().brighter());
    		g2d.setPaint(gp);
    		g2d.fillRect(0, 0, width, height);
     
    		if(_ishour){
    			g2d.setPaint(ShinColor.getDefaultOrange());
    			g2d.setStroke(new BasicStroke(2));
    			g2d.drawLine(0, 1, width, 1);
    			g2d.drawLine(0, 0, 0, height);
    		}
    		else if(_isquart){
    			g2d.setPaint(ShinColor.getDefaultOrange());
    			g2d.setStroke(new BasicStroke(2));
    			g2d.drawLine(0, height/2, width, height/2);
    			g2d.drawLine(0, 0, 0, height);
    		}
    		else if(_ishalph){
    			g2d.setPaint(ShinColor.getDefaultOrange());
    			g2d.setStroke(new BasicStroke(2));
    			g2d.drawLine(0, height, width, height);
    			g2d.drawLine(0, 0, 0, height);
    		}
    		if(_hour != null){
    			// Update des positions des composants
    			_hour.setBounds(new Rectangle(0, 0, this.getWidth(), 4 * this.getHeight()/5));
    		}
    		// Restauration de l'ancien mode de peinture
    		g2d.setPaint(old);
    	}
    Le problème maintenant est que je voudrais imprimer cet JTable avec le code

    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
    Properties props = new Properties();
     
        props.put("awt.print.paperSize", "a4");
        props.put("awt.print.destination", "printer");
     
     
        PrintJob pJob = getToolkit().getPrintJob(this,
    			  "Printing_Test", props);
        if (pJob != null)
          {
            Graphics pg = pJob.getGraphics();
            MonJTable.printAll(pg) 
            pg.dispose();
            pJob.end();
          }
    et j'ai une exception

    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
     
    java.lang.ClassCastException: sun.print.ProxyPrintGraphics
    	at clientInterfacePackage.shinGoodies.ShinJTableHour.paintComponent(ShinJTableHour.java:75)
    	at javax.swing.JComponent.printComponent(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.CellRendererPane.paintComponent(Unknown Source)
    	at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
    	at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
    	at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
    	at javax.swing.plaf.ComponentUI.update(Unknown Source)
    	at javax.swing.JComponent.paintComponent(Unknown Source)
    	at clientInterfacePackage.shinGoodies.ShinJTable.paintComponent(ShinJTable.java:302)
    	at javax.swing.JComponent.printComponent(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.print(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.printChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JViewport.paint(Unknown Source)
    	at javax.swing.JComponent.print(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.printChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.print(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.printChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.print(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.printChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.print(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.printChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.print(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.printChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.print(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.printChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JLayeredPane.paint(Unknown Source)
    	at javax.swing.JComponent.print(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.printChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.print(Unknown Source)
    	at java.awt.GraphicsCallback$PrintCallback.run(Unknown Source)
    	at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
    	at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
    	at java.awt.Container.print(Unknown Source)
    	at sun.awt.windows.WComponentPeer.print(Unknown Source)
    	at sun.awt.windows.WCanvasPeer.print(Unknown Source)
    	at sun.awt.windows.WPanelPeer.print(Unknown Source)
    	at java.awt.GraphicsCallback$PeerPrintCallback.run(Unknown Source)
    	at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
    	at java.awt.Component.printAll(Unknown Source)
    	at clientInterfacePackage.MainFrame.actionPerformed(MainFrame.java:660)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.AbstractButton.doClick(Unknown Source)
    	at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    	at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    qui se rapporte à la ligne dans laquelle je caste le graphics en graphics2D
    Je ne sais pas quoi faire!

  2. #2
    Membre Expert
    Avatar de xavlours
    Inscrit en
    Février 2004
    Messages
    1 832
    Détails du profil
    Informations forums :
    Inscription : Février 2004
    Messages : 1 832
    Par défaut
    Bonjour,

    c'est un problème qui arrive de temps en temps : les drivers de certaines imprimantes font que la méthode paintComponent reçoit un Graphics et non un Graphics2D.

    Dans ce cas, la meilleure chose à faire est de passer par une BufferedImage lorsque c'est nécessaire :
    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
    public void paintComponent(Graphics g) {
     
      BufferedImage img = null;
      Graphics2D g2;
     
      if(g instanceof Graphics2D) {
        g2 = (Graphics2D) g;
      } else {
        img = new BufferedImage(this.getWidth(), this.getHeight(),
                    BufferedImage.TYPE_INT_RGB);
        g2 = img.createGraphics();
      }
     
      // on dessine le composant en utilisant g2
      // de manière classique
     
      super.paintComponent(g2);
      ...
     
     
      if(g != g2) // si on a utilisé l'image
        g.drawImage(img, 0, 0, null);
    }
    "Le bon ni le mauvais ne me feraient de peine si si si je savais que j'en aurais l'étrenne." B.V.
    Non au langage SMS ! Je ne répondrai pas aux questions techniques par MP.
    Eclipse : News, FAQ, Cours, Livres, Blogs.Et moi.

Discussions similaires

  1. Imprimer le contenu d'une JTable?
    Par Sancho_54 dans le forum Composants
    Réponses: 5
    Dernier message: 10/12/2019, 21h22
  2. [VB.NET]Comment Imprimer une form
    Par bernard06 dans le forum Windows Forms
    Réponses: 4
    Dernier message: 19/10/2011, 17h43
  3. focus dans une cellule d'une JTable
    Par coil dans le forum Composants
    Réponses: 5
    Dernier message: 07/07/2010, 17h15
  4. imprimer une JTable
    Par Stratovarius dans le forum Composants
    Réponses: 7
    Dernier message: 01/12/2006, 18h37
  5. Comment imprimer une JTable
    Par L4BiN dans le forum Composants
    Réponses: 9
    Dernier message: 03/08/2006, 15h09

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