Printing Embedded JComponents

Reply

Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 7
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Printing Embedded JComponents

 
0
  #1
May 15th, 2005
Hi everyone,

I have a JTextPane with an embedded JLabel and i am trying to print the contents of the JTextPane. When i print it an exception gets thrown saying that there is an error in the views.

This only happens if i have an embedded jcomponent in the jTextPane but other than that it prints fine with no problems. I thinking i have done something wrong with the views and have tried manipulating but nothing seems to work. I not really sure what i am doing wrong so i am listing a runnable example so you guys can run the program and see what i mean.

  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
  5. import java.util.*;
  6. import java.awt.print.*;
  7. import javax.swing.*;
  8. import javax.swing.event.*;
  9. import javax.swing.text.*;
  10. import javax.swing.plaf.basic.*;
  11. import javax.swing.text.rtf.*;
  12.  
  13. public class JWord implements ActionListener, CaretListener
  14. {
  15.  
  16. JFrame fr = new JFrame ("Frame");
  17. JLabel Label1 = new JLabel("Label1 ", SwingConstants.RIGHT);
  18.  
  19. JLabel Label2 = new JLabel("Hello World ", SwingConstants.RIGHT);
  20.  
  21. JButton Button1 = new JButton("Print");
  22.  
  23. JTextPane TextPane1 = new JTextPane();
  24.  
  25. //The below two command lines creates instances for fonts
  26.  
  27. SimpleAttributeSet sas = new SimpleAttributeSet();
  28.  
  29. StyleContext sc = new StyleContext();
  30.  
  31. //The below command line sets up the variable for font updating
  32.  
  33. MutableAttributeSet mas;
  34.  
  35. //Then below command line sets the style for the JTextPane document
  36.  
  37. DefaultStyledDocument dse = new DefaultStyledDocument(sc);
  38. JScrollPane ScrollPane1 = new JScrollPane(TextPane1, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
  39. ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  40.  
  41. //The below command line is the constructor for the rtf editor kit
  42.  
  43. RTFEditorKit rtfkit = new RTFEditorKit();
  44.  
  45. Dimension Size1 = new Dimension();
  46.  
  47. //The below external class helps in the printing of the rtf document
  48.  
  49. RTFRenderer RTFRenderer1 = new RTFRenderer();
  50.  
  51. public void initialize ()
  52. {
  53. Container pane = fr.getContentPane();
  54. pane.setLayout(new FlowLayout());
  55. fr.setSize(250,300);
  56. fr.setLocation(300,300);
  57. fr.setBackground(Color.lightGray);
  58. //The below command line is the JTextPane using the rtf editor kit
  59. //for any rtf editing
  60.  
  61. TextPane1.setEditorKit(rtfkit);
  62. //The below command line sets the document that the JTextPane will be
  63. //be referencing to
  64.  
  65. TextPane1.setDocument(dse);
  66. Size1.width = 500;
  67. Size1.height = 300;
  68. ScrollPane1.setPreferredSize(Size1);
  69. pane.add(ScrollPane1);
  70. TextPane1.insertComponent(Label2);
  71. pane.add(Button1);
  72. pane.add(Label1);
  73. // Use the JAVA constant JFrame.HIDE_ON_CLOSE for subsequent forms in multi form
  74. // applications
  75.  
  76. fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  77. Button1.addActionListener(this);
  78. TextPane1.addCaretListener(this);
  79. fr.pack();
  80. fr.setVisible(true);
  81. }
  82.  
  83. public void printData()
  84. {
  85. //This is the main printing function that prints out the document
  86.  
  87. //The below command line sets the current JTextPane for printing
  88. //by using the external printing class
  89.  
  90. RTFRenderer1.setpane(TextPane1);
  91.  
  92. //The below command line gets the attributes for the JAVA print dialog
  93.  
  94. try
  95. {
  96. //The below two command lines gets the Printer Job and uses the
  97. //external RTFRenderer class as its Printable
  98.  
  99. PrinterJob prnJob = PrinterJob.getPrinterJob();
  100. PageFormat format = prnJob.pageDialog(prnJob.defaultPage());
  101. prnJob.setPrintable(RTFRenderer1, format);
  102.  
  103. if (prnJob.printDialog() == false)
  104. {
  105. return;
  106. }
  107.  
  108. //The below command line prints out the document if the user clicked Ok
  109.  
  110. prnJob.print();
  111. }
  112.  
  113. catch (PrinterException e)
  114. {
  115. Label1.setText("A printing error has occurred");
  116. }
  117.  
  118. }
  119.  
  120. public void actionPerformed(ActionEvent event)
  121. {
  122. JComponent b = (JComponent)event.getSource();
  123.  
  124. if(b == Button1)
  125. {
  126. //The below two command lines prints the current content of the document
  127. //and repaints the main frame
  128.  
  129. printData();
  130. fr.repaint();
  131. }
  132.  
  133. }
  134.  
  135. public void caretUpdate(CaretEvent event)
  136. {
  137. JComponent w = (JComponent)event.getSource();
  138. int g;
  139.  
  140. if(w == TextPane1)
  141. {
  142. g = event.getDot();
  143. Label1.setText("Current Caret Position: " + g);
  144. }
  145.  
  146. }
  147. public static void main(String args[])
  148. {
  149. JWord a = new JWord();
  150. a.initialize();
  151. }
  152. }
  153.  
  154. class RTFRenderer implements Printable
  155. {
  156. //This external class that does all the printing of all the rtf document pages
  157.  
  158. int currentPage = -1;
  159. JTextPane jtextPane = new JTextPane();
  160. double pageEndY = 0;
  161. double pageStartY = 0;
  162. boolean scaleWidthToFit = true;
  163. int currentIndex = 0;
  164.  
  165. public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
  166. {
  167. //This function is the main printable overided function
  168.  
  169. double scale = 1.0;
  170. Graphics2D graphics2D;
  171. View rootView;
  172. graphics2D = (Graphics2D) graphics;
  173.  
  174. jtextPane.setSize((int)pageFormat.getImageableWidth(),Integer.MAX_VALUE);
  175. jtextPane.validate();
  176. //I think the error is somwhere here
  177.  
  178. rootView = jtextPane.getUI().getRootView(jtextPane);
  179.  
  180. if((scaleWidthToFit) && (jtextPane.getMinimumSize().getWidth() >
  181. pageFormat.getImageableWidth()))
  182. {
  183. scale = pageFormat.getImageableWidth()/jtextPane.getMinimumSize().getWidth();
  184. graphics2D.scale(scale, scale);
  185. }
  186.  
  187. //The below four command lines shows that the content is clipped
  188. //to the size of the printable page
  189.  
  190. graphics2D.setClip((int)(pageFormat.getImageableX()/scale),
  191. (int)(pageFormat.getImageableY()/scale),
  192. (int)(pageFormat.getImageableWidth()/scale),
  193. (int)(pageFormat.getImageableHeight()/scale));
  194.  
  195. //The below if statement is to check to see if there is a new page to render
  196.  
  197. if(pageIndex > currentPage)
  198. {
  199. currentPage = pageIndex;
  200. pageStartY += pageEndY;
  201. pageEndY = graphics2D.getClipBounds().getHeight();
  202. }
  203.  
  204. graphics2D.translate(graphics2D.getClipBounds().getX(),
  205. graphics2D.getClipBounds().getY());
  206.  
  207. Rectangle allocation = new Rectangle(0, (int)-pageStartY,
  208. (int)(jtextPane.getMinimumSize().getWidth()),
  209. (int)(jtextPane.getPreferredSize().getHeight()));
  210.  
  211. //The below if else statements return PAGE_EXISTS only if the class
  212. //sees that there are some contents in the document by calling the printView class
  213.  
  214. if(printView(graphics2D,allocation,rootView))
  215. {
  216. return PAGE_EXISTS;
  217. }
  218.  
  219. else
  220. {
  221. pageStartY = 0;
  222. pageEndY = 0;
  223. currentPage = -1;
  224. currentIndex = 0;
  225. return NO_SUCH_PAGE;
  226. }
  227.  
  228. }
  229.  
  230. protected boolean printView(Graphics2D graphics2D, Shape allocation, View view)
  231. {
  232. //This function paints the page if it exists
  233.  
  234. boolean pageExists = false;
  235. Rectangle clipRectangle = graphics2D.getClipBounds();
  236. Shape childAllocation;
  237. View childView;
  238.  
  239. if(view.getViewCount() > 0)
  240. {
  241.  
  242. for(int i = 0;i<view.getViewCount();i++)
  243. {
  244.  
  245. childAllocation = view.getChildAllocation(i,allocation);
  246. if (childAllocation != null)
  247. {
  248. childView = view.getView(i);
  249.  
  250. if(printView(graphics2D,childAllocation,childView))
  251. {
  252. pageExists = true;
  253. }
  254.  
  255. }
  256.  
  257. }
  258.  
  259. }
  260.  
  261. else
  262. {
  263. //The below if statement checks if there are pages currently to paint
  264.  
  265. if(allocation.getBounds().getMaxY() >= clipRectangle.getY())
  266. {
  267. pageExists = true;
  268.  
  269. if((allocation.getBounds().getHeight() > clipRectangle.getHeight()) &&
  270. (allocation.intersects(clipRectangle)))
  271. {
  272. view.paint(graphics2D,allocation);
  273. }
  274.  
  275. else
  276. {
  277.  
  278. if(allocation.getBounds().getY() >= clipRectangle.getY())
  279. {
  280.  
  281. if(allocation.getBounds().getMaxY() <= clipRectangle.getMaxY() - 15)
  282. {
  283. view.paint(graphics2D,allocation);
  284. }
  285.  
  286. else
  287. {
  288.  
  289. if(allocation.getBounds().getY() < pageEndY)
  290. {
  291. pageEndY = allocation.getBounds().getY();
  292. }
  293.  
  294. }
  295.  
  296. }
  297.  
  298. }
  299.  
  300. }
  301.  
  302. }
  303.  
  304. return pageExists;
  305. }
  306.  
  307. public void setpane(JTextPane TextPane1)
  308. {
  309. //This function gets the JTextPane
  310.  
  311. jtextPane.setContentType("text/rtf");
  312. jtextPane = TextPane1;
  313. }
  314.  
  315. }

I am using the default styled document together with rtf editor kit and the content of the JTextPane is set to rtf.

It seems that i can print everthing else including embedded images and rtf text except an embedded jcomponent and maybe someone could have a look at my code and see what i am doing wrong and help me
print multi-page embedde jcomponents in the JTextPane

I hope someone can help me with this problem

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West
Microsoft uses "One World, One Web, One Program" as a slogan.
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond

Tell me what type of software do you like and what would you pay for it

http://www.daniweb.com/techtalkforums/thread19660.html
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC