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

Applets Java Discussion :

Imprimer le contenu d'un JEditorPane


Sujet :

Applets Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre très actif Avatar de DarkSeiryu
    Homme Profil pro
    Développeur Java
    Inscrit en
    Janvier 2009
    Messages
    425
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Janvier 2009
    Messages : 425
    Par défaut Imprimer le contenu d'un JEditorPane
    Bonjour tout le monde.

    Comme dit dans le titre, je cherche à imprimer le contenu d'un JEditorPane. Dans ce dernier, je genère du code HTML et je souhaiterais donc imprimer ce dernier et uniquement ce dernier.

    Techniquement parlant, j'y arrive mais mon contenu est coupé sur la droite (il manque une colonne dans ma <table>). Voici comment je m'y prends :

    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
    // Récupère le travail et affiche la boite de dialogue d'impression.
        PrintJob job = getToolkit().getPrintJob(this, "Impression du rapport", null);
        if (job != null) {
    	// Recupere le Graphics dans lequel on va ecrire.
    	Graphics g = job.getGraphics();
    	if (g != null) {
    	    // Imprime les composants du jEditorPane1.
    	    //this.print(g);
    	    this.jEditorPane1.printAll(g);
    	    g.dispose();
    	}
     
    	// On ferme le PrintJob.
    	job.end();
        }
    Ce qui n'imprime donc bien que le contenu de mon JEditorPane, mais coupé. Du coup, j'ai essayé d'une aute manière :

    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
    if (Desktop.isDesktopSupported()) {
            if (Desktop.getDesktop().isSupported(java.awt.Desktop.Action.PRINT)) {
                try {
                    // TODO : penser à changer le path du fichier.
                    File rapportFile = new File(monChemin);
                    System.out.println(rapportFile.getAbsolutePath());
     
                    FileWriter writer = null;		
                    try {
    		    writer = new FileWriter(rapportFile.getAbsolutePath());
    		    writer.write(this.rapport, 0, this.rapport.length());
     
                        java.awt.Desktop.getDesktop().print(rapportFile.getAbsoluteFile());
                    } catch (IOException ioe) {
    		    ioe.printStackTrace();
                    } catch (Exception e) {
    		    e.printStackTrace();
    		} finally {
    		    if (writer != null)
    			writer.close();
                    }
                } catch (IOException ioe) {
    		ioe.printStackTrace();
                } catch (Exception e) {
    		e.printStackTrace();
    	    }
    	}
    	else {
                //La fonction n'est pas supportée par votre systême d'exploitation
    	}
        }
        else {
            //Desktop pas supporté par votre systême d'exploitation
        }
    Je sauvegarde donc mon contenu dans un fichier HTML et j'essaie d'imprimer ce dernier. Le souci, c'est que je n'arrive pas à imprimer ce fichier (java.io.Exception : Error Message : Erreur inconnue).

    Quelqu'un aurait une meilleure solution à me proposer ou une correction à mon code SVP ?

    Merci d'avance !

    DarkSeiryu

  2. #2
    Membre chevronné Avatar de Drakiss
    Homme Profil pro
    Consultant MOE
    Inscrit en
    Octobre 2010
    Messages
    185
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Consultant MOE
    Secteur : Finance

    Informations forums :
    Inscription : Octobre 2010
    Messages : 185
    Par défaut
    D'ou ça sort ce (java.io.Exception : Error Message : Erreur inconnue) ??
    C'est un null ou tu n'as pas mis toute la stacktrace ?

  3. #3
    Membre très actif Avatar de DarkSeiryu
    Homme Profil pro
    Développeur Java
    Inscrit en
    Janvier 2009
    Messages
    425
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Janvier 2009
    Messages : 425
    Par défaut
    J'aurais du donner la StackTrace complète directement. ^^

    java.io.IOException: Failed to print file:monFichier.html. Error message: Erreur non spécifiée

    at sun.awt.windows.WDesktopPeer.ShellExecute(Unknown Source)
    at sun.awt.windows.WDesktopPeer.print(Unknown Source)
    at java.awt.Desktop.print(Unknown Source)
    at UI.Composent.RapportJFrame.jButtonPrintActionPerformed(RapportJFrame.java:164)
    at UI.Composent.RapportJFrame.access$100(RapportJFrame.java:24)
    at UI.Composent.RapportJFrame$2.actionPerformed(RapportJFrame.java:66)
    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.plaf.basic.BasicButtonListener.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.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(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)

  4. #4
    Membre chevronné Avatar de Drakiss
    Homme Profil pro
    Consultant MOE
    Inscrit en
    Octobre 2010
    Messages
    185
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Consultant MOE
    Secteur : Finance

    Informations forums :
    Inscription : Octobre 2010
    Messages : 185
    Par défaut
    La stackTrace te dis que java.awt.Desktop.print(Unknown Source).
    Il ne trouve donc pas ce que tu veux printer.

    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
     
    try {
          Desktop desktop = null;
          if (Desktop.isDesktopSupported()) {
            desktop = Desktop.getDesktop();
            try {
    		    writer = new FileWriter(rapportFile.getAbsolutePath());
    		    writer.write(this.rapport, 0, this.rapport.length());
                        System.out.println("The " + rapportFile.getAbsolutePath() + " file created");
                    } catch (IOException ioe) {
    		    //ioe.printStackTrace();
                        System.out.println("Cannot write the " + rapportFile.getAbsolutePath() + " file");
                }finally {
    		    if (writer != null)
    			writer.close();
                    }
     
           desktop.print(new File("monChemin"));
           System.out.println("File " + file + " printed")
           }else
          System.out.println("Desktop not supported...");
     
        } catch (IOException ioe) {
          //ioe.printStackTrace();
                System.out.println("Cannot perform the given operation 
                    to the " + file + " file");
    Bon y'a surement plein d'erreur dans le code, mais le principal est là : flush ou close ton writer avant d'essayer de récupérer le fichier et test si Desktop est supporté (windows ok, mais linux non par ex)...

  5. #5
    Membre très actif Avatar de DarkSeiryu
    Homme Profil pro
    Développeur Java
    Inscrit en
    Janvier 2009
    Messages
    425
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Janvier 2009
    Messages : 425
    Par défaut
    J'ai fait les modif' et ai retesté. Même exception :
    The "C:\Users\Mickaël\Desktop29-09-11_15-46.html" file has been created.
    Cannot perform the given operation to the "C:\Users\Mickaël\Desktop29-09-11_15-46.html" file. Stacktrace :
    java.io.IOException: Failed to print file:/C:/Users/Mickaël/Desktop29-09-11_15-46.html. Error message: Erreur non spécifiée

    at sun.awt.windows.WDesktopPeer.ShellExecute(Unknown Source)
    at sun.awt.windows.WDesktopPeer.print(Unknown Source)
    at java.awt.Desktop.print(Unknown Source)
    at UI.Composent.RapportJFrame.jButtonPrintActionPerformed(RapportJFrame.java:210)
    at UI.Composent.RapportJFrame.access$100(RapportJFrame.java:24)
    at UI.Composent.RapportJFrame$2.actionPerformed(RapportJFrame.java:66)
    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.plaf.basic.BasicButtonListener.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.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(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)
    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
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    File rapportFile = new File("C:/Users/Mickaël/Desktop" + new SimpleDateFormat("dd-MM-yy_HH-mm").format(new Date()) + ".html");
        System.out.println("The \"" + rapportFile.getAbsolutePath() + "\" file has been created.");
     
        try {
    	Desktop desktop = null;
    	if (Desktop.isDesktopSupported()) {
    	    desktop = Desktop.getDesktop();
     
    	    FileWriter writer = null;		
    	    try {
    		writer = new FileWriter(rapportFile.getAbsolutePath());
    		writer.write(this.rapport, 0, this.rapport.length());
    	    } catch (IOException ioe) {
    		System.out.println("Cannot write the \"" + rapportFile.getAbsolutePath() + "\" file. Stacktrace :");
    		ioe.printStackTrace();
    	    } catch (Exception e) {
    		e.printStackTrace();
    	    } finally {
    		if (writer != null)
    		    writer.close();
    	    }
     
    	    desktop.print(rapportFile.getAbsoluteFile());
    	}
    	else {
    	    System.out.println("Desktop non supporté par votre systême d'exploitation.");
    	}
     
        } catch (IOException ioe) {
    	System.out.println("Cannot perform the given operation to the \"" + rapportFile.getAbsolutePath() + "\" file. Stacktrace :");
    	ioe.printStackTrace();
        } catch (Exception e) {
    	e.printStackTrace();
        }
    EDIT : je précise que j'ai essayé de générer le fichier sur serveur (qui est sous Debian) et en local sur mon poste (Windows 7).

  6. #6
    Membre chevronné Avatar de Drakiss
    Homme Profil pro
    Consultant MOE
    Inscrit en
    Octobre 2010
    Messages
    185
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Consultant MOE
    Secteur : Finance

    Informations forums :
    Inscription : Octobre 2010
    Messages : 185
    Par défaut
    Ca serait pas ton ë de mickael qui passe pas ??
    Ou alors les / dans le path du fichier, ça merdoit toujours mais je sait jamais comment les foutre si c'est des /ou\ou des //ou\\
    Apparement test aussi :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    File absolute = new File("/temp/Desktop.html");
    File relative = new File("/temp/Desktop.html");
    Fout le à la racine du c: simplifie le nom j'ai un doute et vérifie que le fichier est bien là, on va y arriver !!
    Edit : Debian je connais pas, mais sous seven y'a surement un problème de droits admin aussi...

    euh d'ailleurs ton desktop.print accepte que les File, pourquoi vouloir lui donner le classPath du fichier ??

Discussions similaires

  1. Je veux imprimer le contenu de ma combo ?!!
    Par nil dans le forum Bases de données
    Réponses: 2
    Dernier message: 29/03/2004, 16h17
  2. imprimer le contenu d'une TPaintBox
    Par stimbo dans le forum C++Builder
    Réponses: 2
    Dernier message: 15/03/2004, 20h42
  3. [Kylix] Imprimer le contenu d'une boîte de dialogue
    Par cmp-france dans le forum EDI
    Réponses: 13
    Dernier message: 18/10/2003, 21h31
  4. imprimer le contenu de l'écran
    Par ensi dans le forum C
    Réponses: 10
    Dernier message: 19/08/2003, 11h47
  5. Comment imprimer le contenu d'un TStringGrid
    Par scorpiwolf dans le forum C++Builder
    Réponses: 2
    Dernier message: 19/06/2002, 16h41

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