Spacings And Carriage Returns

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

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

Spacings And Carriage Returns

 
0
  #1
Sep 7th, 2006
Hi everyone,

I currently have a JTextPane using a html document and i am trying to serialize the html document contained in the JTextPane to the disk but i have run into some problems.

One problem lets say for example if i were to press the space bar five times and then type some text into the JTextPane and serialize it, everything seems alright except that when i read back the html document the space where i had pressed for five times has dissappeared and the text that i typed in is just left justified.

Another problem i have lets say for example if i were to press the carriage return five times and then type some text into the JTextPane and serialize it, everything seems alright except that when i read back the html document the carriage returns where i had pressed for five times has dissappeared and the text that i typed in is just placed at the top of the document in the JTextPane.

Why this is happening i am not sure but i am providing a compilable example for you guys to see what actually the problems that i am currently facing.

Here is the compilable example.

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. import java.util.*;
  5. import javax.swing.*;
  6. import javax.swing.event.*;
  7. import javax.swing.text.*;
  8. import javax.swing.text.html.*;
  9.  
  10. public class JHTML implements ActionListener
  11.  
  12. {
  13.  
  14. JFrame fr = new JFrame ("Frame");
  15.  
  16. JLabel Label1 = new JLabel("Label1 ", SwingConstants.RIGHT);
  17.  
  18. JButton Button1 = new JButton("Load");
  19. JButton Button2 = new JButton("Save As");
  20.  
  21. JFileChooser FileChooser1 = new JFileChooser();
  22. JFileChooser FileChooser2 = new JFileChooser();
  23.  
  24. JTextPane TextPane1 = new JTextPane();
  25.  
  26. StyleSheet ss = new StyleSheet();
  27.  
  28. HTMLDocument htmldoc = new HTMLDocument(ss);
  29.  
  30. JScrollPane ScrollPane1 = new JScrollPane(TextPane1, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
  31. ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  32.  
  33. HTMLEditorKit htmlkit = new HTMLEditorKit();
  34.  
  35. Dimension Size1 = new Dimension();
  36.  
  37. String SF = "";
  38.  
  39. public void initialize ()
  40. {
  41. Container pane = fr.getContentPane();
  42. pane.setLayout(new FlowLayout());
  43. fr.setSize(250,300);
  44. fr.setLocation(300,300);
  45. fr.setBackground(Color.lightGray);
  46. TextPane1.setEditorKit(htmlkit);
  47. htmldoc = (HTMLDocument)htmlkit.createDefaultDocument();
  48. ss = htmldoc.getStyleSheet();
  49. TextPane1.setDocument(htmldoc);
  50.  
  51. Size1.width = 500;
  52. Size1.height = 300;
  53. ScrollPane1.setPreferredSize(Size1);
  54. pane.add(ScrollPane1);
  55. pane.add(Button1);
  56. pane.add(Button2);
  57. pane.add(Label1);
  58.  
  59. fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  60. Button1.addActionListener(this);
  61. Button2.addActionListener(this);
  62. fr.pack();
  63. fr.setVisible(true);
  64. }
  65.  
  66. public void open ()
  67. {
  68.  
  69.  
  70. if(FileChooser1.showOpenDialog(fr) != JFileChooser.APPROVE_OPTION)
  71. {
  72. return;
  73. }
  74.  
  75. try
  76. {
  77. File file1 = FileChooser1.getSelectedFile();
  78. SF = file1.toString();
  79. FileInputStream in = new FileInputStream(SF);
  80. htmldoc = (HTMLDocument)htmlkit.createDefaultDocument();
  81. htmlkit.read(in, htmldoc, 0);
  82. ss = htmldoc.getStyleSheet();
  83. TextPane1.setDocument(htmldoc);
  84. in.close();
  85. }
  86.  
  87. catch (Exception e)
  88. {
  89. Label1.setText("A document reading error has occured");
  90. }
  91.  
  92. }
  93.  
  94. public void saveas ()
  95. {
  96. if(FileChooser1.showSaveDialog(fr) != JFileChooser.APPROVE_OPTION)
  97. {
  98. return;
  99. }
  100.  
  101. try
  102. {
  103. File file2 = FileChooser1.getSelectedFile();
  104. SF = (file2.toString() + ".html");
  105. FileOutputStream out = new FileOutputStream(SF);
  106. htmlkit.write(out, htmldoc, 0, htmldoc.getLength());
  107. out.close();
  108. }
  109.  
  110. catch (Exception e)
  111. {
  112. Label1.setText("A document writing error has occured");
  113. }
  114.  
  115. }
  116.  
  117. public void actionPerformed(ActionEvent event)
  118. {
  119. JComponent b = (JComponent)event.getSource();
  120.  
  121. if(b == Button1)
  122. {
  123. open();
  124. }
  125.  
  126. else if(b == Button2)
  127. {
  128. saveas();
  129. }
  130.  
  131.  
  132. }
  133. public static void main(String args[])
  134. {
  135. JHTML a = new JHTML();
  136. a.initialize();
  137. }
  138. }

My only idea i guess is that i must be serializing the document wrongly, but i tried to yahoo but came up with nothing.

I hope someone knows why this is happening as already i am out of ideas.

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  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Spacings And Carriage Returns

 
0
  #2
Sep 7th, 2006
when parsing html browsers will remove leading and trailing whitespace. That's part of the required behaviour for them.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
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