Bonjour,
Je suis en train d'essayer d'imprimer mon JEditorPane mais je rencontre un soucis lors de l'ajout du JMenuItem Imprimer à mon JMenu.
Voyez vous où se trouve mon erreur svp ?

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
 
FileAfficher() {
		super("File Printer");
		setSize(new Dimension(800, 800));
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				quitter();
			}
		});
 
		texte = new JEditorPane();
		texte.setContentType("text/html" );
		texte.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
 
		StyleSheet myStyleSheet = new StyleSheet();
	    myStyleSheet.addRule("body {font: 20px Dialog; color: blue}");
	    myStyleSheet.addRule("p {font: 20px Serif}");
 
	    HTMLEditorKit k = (HTMLEditorKit)(texte.getEditorKitForContentType("text/html"));
	    k.setStyleSheet(myStyleSheet);
	    texte.setEditorKit(k);
 
	    Document doc = texte.getDocument();
	    texte.setDocument(doc);
 
		/*HTMLEditorKit k = new HTMLEditorKit();
		doc = (HTMLDocument)k.createDefaultDocument();
		texte.setEditorKit(k);
		texte.setDocument(doc);*/
 
 
		//L'unité de base de la structure est un Element, qui a un ensemble d'attributs. 
		//Les éléments st de natures différentes suivant qu'on a affaire un texte HTML : html, body, p, content, etc...
 
		//texte.setFont(Monospaced);
 
		/**
                Style defaut = texte.getStyle("default");
                Style as = texte.addStyle("stylePerso",defaut);
 
                StyleConstants.setFontFamily(as, "Monospaced");
                StyleConstants.setFontSize(as, 20);
                StyleConstants.setAlignment(as, 1);
 
                StyledDocument docStyled = texte.getStyledDocument();**/
 
		texte.setBackground(Color.WHITE);
 
 
 
		texte.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent evt) {
				texteModifie = true;
			}
		});
 
		getContentPane().add(new JScrollPane(texte));
		setJMenuBar(barreMenus());
		setVisible(true);
	}
 
	private JMenuBar barreMenus() {
		JMenuBar barre = new JMenuBar();
 
		JMenu menu = new JMenu("Fichier");
		barre.add(menu);
 
		JMenuItem nouveau = new JMenuItem("Nouveau");
		menu.add(nouveau);
		nouveau.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				nouveau();
			}
		});
 
		JMenuItem ouvrir = new JMenuItem("Ouvrir...");
		menu.add(ouvrir);
		ouvrir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				ouvrir();
			}
		});
 
		menu.addSeparator();
		JMenuItem test = new JMenuItem("test...");
		menu.add(test);
 
		JMenuItem enregistrer = new JMenuItem("Enregistrer sous...");
		menu.add(enregistrer);
		enregistrer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				enregistrer();
			}
		});
 
		JMenuItem imprimer = new JMenuItem("Imprimer");
		menu.add(imprimer);
		imprimer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				PrinterJob printJob = PrinterJob.getPrinterJob();
				printJob.setPrintable((Printable) texte);
				if(printJob.printDialog())
				{
					try
						{printJob.print();
						}
					catch (Exception pe){
						System.out.println(pe);
						}
				}
			}
		});
 
		menu.addSeparator();
		JMenuItem quitter = new JMenuItem("Quitter");
		menu.add(quitter);
		quitter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				quitter();
			}
		});
 
		JMenuItem apropos = new JMenuItem("A propos du File Printer");
		menu.add(apropos);
		apropos.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				aPropos();
			}
		});
 
		menu.addSeparator();
 
		JMenuItem information = new JMenuItem("Information");
		menu.add(information);
		information.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String nomCentre= JOptionPane.showInputDialog("Nom du centre hospitalier: ");
				String idCentre= JOptionPane.showInputDialog("Identifiant du centre hospitalier: ");
 
 
				// ici pour insérer un nouveau préleveur dans le listing
				String nomPreleveur = (String) JOptionPane.showInputDialog(null,
	                    "Préleveur: ",
	                    "Préleveur",
	                    JOptionPane.INFORMATION_MESSAGE,
	                    null,
	                    new Object[] { "NCV", "SPA", "Alban"},
	                    "NCV"
	                    );
 
				String datePrelev=JOptionPane.showInputDialog("Date de prélèvement:");
				String heureArrivee=JOptionPane.showInputDialog("Heure d'arrivée:");
				String heureDepart=JOptionPane.showInputDialog("Heure de départ:");
 
				//penser à convertir si besoin numeroPage en int
				String numeroPage = (String) JOptionPane.showInputDialog(null,
	                    "Numero de fiche: ",
	                    "Numero de fiche",
	                    JOptionPane.INFORMATION_MESSAGE,
	                    null,
	                    new Object[] { "01", "02", "03"},
	                    "01"
	                    );
 
				try {
					try {
						FicheRecap recap = new FicheRecap(nomCentre,idCentre,nomPreleveur,datePrelev,heureArrivee,heureDepart,numeroPage);
					} catch (BadLocationException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					texteModifie =true;
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
 
			}
		});
 
 
		return barre;
	}
J'ai essayé d'ajouter un Item test tout bête qui ne marche pas non plus
Voilà le script qui est appelé lorsqu'on clique sur imprimer :

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
 
import java.awt.*;
 
public class PrintPane extends JTextPane implements Printable,Serializable
{
	JLabel label;
	JTextPane jtextpane;
	JButton browse,print;
	JPanel p;
 
	public PrintPane(){}
	public int print(Graphics g,PageFormat pf,int pi) throws PrinterException
	{
 
		Graphics2D g2 = (Graphics2D)g;
		g2.setColor(Color.black);
 
		Dimension d = this.getSize(); //get size of document
		double panelWidth = d.width; //width in pixels
		double panelHeight = d.height; //height in pixels
 
		double pageHeight = pf.getImageableHeight(); //height of printer page
		double pageWidth = pf.getImageableWidth(); //width of printer page
 
		double scale = pageWidth/panelWidth;
		int totalNumPages = (int)Math.ceil(scale * panelHeight / pageHeight);
 
		//make sure not print empty pages
		if(pi >= totalNumPages)
		{ return Printable.NO_SUCH_PAGE;
		}
 
		//shift Graphic to line up with beginning of print-imageable region
		g2.translate(pf.getImageableX(), pf.getImageableY());
 
		//shift Graphic to line up with beginning of next page to print
		g2.translate(0f, - pi * pageHeight);
 
		//scale the page so the width fits...
		g2.scale(scale, scale);
 
		this.paint(g2); //repaint the page for printing
 
		return Printable.PAGE_EXISTS;
 
		/*if (pi>=0)
		{
			return Printable.NO_SUCH_PAGE;
		}
		g.translate(100,100);
		paint(g);
		return Printable.PAGE_EXISTS;*/
	}
}
Voilà!!
Merci pour votre aide